-
Notifications
You must be signed in to change notification settings - Fork 4k
feat: Add Term Entry for C++ Arrays begin() #6808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a497187
feat: Add term entry for C++ Arrays begin()
Kenxpx b270421
Update content/cpp/concepts/arrays/terms/begin/begin.md
Kenxpx 3f680da
Update content/cpp/concepts/arrays/terms/begin/begin.md
Kenxpx 749d31d
Update content/cpp/concepts/arrays/terms/begin/begin.md
Kenxpx ab245a9
Update content/cpp/concepts/arrays/terms/begin/begin.md
Kenxpx ce43bd7
Update content/cpp/concepts/arrays/terms/begin/begin.md
Kenxpx adce3dc
Update content/cpp/concepts/arrays/terms/begin/begin.md
Kenxpx b39a12d
Update content/cpp/concepts/arrays/terms/begin/begin.md
Kenxpx 573a50b
Update content/cpp/concepts/arrays/terms/begin/begin.md
Kenxpx 4af8b60
Update content/cpp/concepts/arrays/terms/begin/begin.md
Kenxpx f2d0c20
Merge branch 'main' into cpp-arrays-begin-entry
Kenxpx 5d18442
Merge branch 'main' into cpp-arrays-begin-entry
Kenxpx e2125c5
minor fix
mamtawardhani abcd750
Merge branch 'main' into cpp-arrays-begin-entry
Kenxpx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
--- | ||
Title: '.begin()' | ||
Description: 'Returns an iterator pointing to the first element of a C++ array or container.' | ||
Subjects: | ||
- 'Code Foundations' | ||
- 'Computer Science' | ||
Tags: | ||
- 'Arrays' | ||
- 'Iterators' | ||
- 'Pointers' | ||
CatalogContent: | ||
- 'learn-c-plus-plus' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
The **`.begin()`** method in C++ returns an iterator pointing to the first element of a container, such as `std::array`, or of a C-style array when used with the free function `std::begin()`. This iterator can be used to access or iterate through the array elements. | ||
|
||
## Syntax | ||
|
||
The usage of `.begin()` depends on the type of array: | ||
|
||
1\. For `std::array` (from `<array>`, introduced in C++11): | ||
|
||
```pseudo | ||
std_array_object.begin() | ||
``` | ||
|
||
**Return value:** | ||
|
||
Returns an iterator of the appropriate type, pointing to the first element of the `std_array_object`. If the array is empty, the returned iterator will be equal to the iterator returned by `std_array_object.end()`. | ||
|
||
2\. For C-style arrays (using `std::begin` from the `<iterator>` header): | ||
|
||
```pseudo | ||
std::begin(c_style_array_name) | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `c_style_array_name`: The name of the C-style array. | ||
|
||
**Return Value:** | ||
|
||
Returns a pointer (which acts as an iterator for C-style arrays) to the first element of `c_style_array_name`. For an empty C-style array (0 elements, though less common), its behavior is specific to how such an array is defined. | ||
|
||
## Example | ||
|
||
Here's how `.begin()` can be used with an `std::array`: | ||
|
||
```cpp | ||
#include <iostream> | ||
#include <array> // Required for std::array | ||
#include <iterator> // Required for std::begin (for C-style arrays example) | ||
|
||
int main() { | ||
// Example with std::array | ||
std::array<int, 5> numbers = {10, 20, 30, 40, 50}; | ||
|
||
// Get an iterator to the first element | ||
std::array<int, 5>::iterator it = numbers.begin(); | ||
// Or using auto for type deduction: | ||
// auto it = numbers.begin(); | ||
|
||
// Access the first element | ||
if (it != numbers.end()) { // Good practice: check if the array is not empty | ||
std::cout << "First element of std::array: " << *it << std::endl; | ||
} | ||
|
||
// Example with a C-style array using std::begin() | ||
int c_style_numbers[] = {5, 15, 25}; | ||
int* ptr_begin = std::begin(c_style_numbers); | ||
|
||
// Access the first element | ||
// (Assuming c_style_numbers is not empty) | ||
std::cout << "First element of C-style array: " << *ptr_begin << std::endl; | ||
} | ||
``` | ||
|
||
This example would output: | ||
|
||
```shell | ||
First element of std::array: 10 | ||
First element of C-style array: 5 | ||
``` | ||
|
||
## Codebyte Example | ||
|
||
The following example demonstrates the use of `std::array::begin()` to access and print the first element of an `std::array`. | ||
|
||
```codebyte/cpp | ||
#include <iostream> | ||
#include <array> | ||
|
||
int main() { | ||
std::array<int, 3> myArray = {100, 200, 300}; | ||
|
||
// Get an iterator to the beginning of the array | ||
auto it = myArray.begin(); | ||
|
||
// Check if the array is not empty and print the first element | ||
if (it != myArray.end()) { | ||
std::cout << "The first element is: " << *it << std::endl; | ||
} else { | ||
std::cout << "The array is empty." << std::endl; | ||
} | ||
|
||
// You can also use the iterator to traverse the array | ||
std::cout << "Array elements: "; | ||
for (auto current_it = myArray.begin(); current_it != myArray.end(); ++current_it) { | ||
std::cout << *current_it << " "; | ||
} | ||
std::cout << std::endl; | ||
|
||
return 0; | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.