array::begin() and array::end() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays.
begin() function is used to return an iterator pointing to the first element of the array container. begin() function returns a bidirectional iterator to the first element of the container.
Syntax :
arrayname.begin() Parameters : No parameters are passed. Returns : This function returns a bidirectional iterator pointing to the first element.
Examples:
Input : myarray{1, 2, 3, 4, 5};
Output : returns an iterator to the element 1
Input : myarray{8, 7};
Output : returns an iterator to the element 8
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// CPP program to illustrate // Implementation of begin() function #include <array> #include <iostream> using namespace std; int main() { // declaration of array container array<int, 5> myarray{ 1, 2, 3, 4, 5 }; // using begin() to print array for (auto it = myarray.begin(); it != myarray.end(); ++it) cout << ' ' << *it; return 0; } |
Output:
1 2 3 4 5
end() function is used to return an iterator pointing to the last element of the array container. end() function returns a bidirectional iterator to the last element of the container.
Syntax :
arrayname.end() Parameters : No parameters are passed. Returns : This function returns a bidirectional iterator pointing to the last element.
Examples:
Input : myarray{1, 2, 3, 4, 5};
Output : returns an iterator to the element 5
Input : myarray{8, 7};
Output : returns an iterator to the element 7
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// CPP program to illustrate // Implementation of end() function #include <array> #include <iostream> using namespace std; int main() { // declaration of array container array<int, 5> myarray{ 1, 2, 3, 4, 5 }; // using end() to print array for (auto it = myarray.begin(); it != myarray.end(); ++it) cout << ' ' << *it; return 0; } |
Output:
1 2 3 4 5
Recommended Posts:
- Synchronous Chatting Application using C++ boost::asio
- C/C++ For loop with Examples
- How to input a comma separated string in C++?
- Program to Parse a comma separated string in C++
- Difference between Private and Protected in C++ with Example
- Difference between Inheritance and Polymorphism
- Difference between Single and Multiple Inheritance in C++
- Difference between Abstraction and Encapsulation in C++
- return statement in C/C++ with Examples
- How to implement our own Vector Class in C++?
- Web Programming in C++
- C++ Program to print an Array using Recursion
- Storage Classes in C++ with Examples
- fill_n() function in C++ STL with examples
- fill() function in C++ STL with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



