Home »
C++ STL
array::begin() and array::end() functions with Example in C++ STL
C++ STL | array::begin() and array::end() functions: Here, we are going to learn about the array::begin() and array::end() functions of Array in C++ STL.
Submitted by IncludeHelp, on March 01, 2019
C++ STL array::begin() and array::end() functions
array::begin() function is a library function of array and it is used to get the first element of the array, it returns an iterator pointing to the first element of the array.
array::end() function is a library function of array and it is used to get the last element of the array, it returns an iterator pointing to the last element of the array.
Syntax:
array::begin();
array::end();
Parameters: None
Return value: Function return iterators pointing to the first and last elements of an array.
Example:
Input or array declaration:
array<int,5> arr {10, 20, 30, 40, 50};
Function call:
auto it=arr.begin();
cout<<*it;
it=arr.end();
cout<<*it;
Output:
10 50
C++ STL program to demonstrate example of array::begin() and array::end() functions
#include <array>
#include <iostream>
using namespace std;
int main()
{
array<int,5> numbers {10, 20, 30, 40, 50};
array<string,5> cities {"New Delhi", "Mumbai", "Gwalior"};
cout<<"Elements of numbers array..."<<endl;
for(auto it=numbers.begin(); it!=numbers.end(); it++)
cout<<*it<<" ";
cout<<endl;
cout<<"Elements of cities array..."<<endl;
for(auto it=cities.begin(); it!=cities.end(); it++)
cout<<*it<<" ";
cout<<endl;
return 0;
}
Output
Elements of numbers array...
10 20 30 40 50
Elements of cities array...
New Delhi Mumbai Gwalior