Open In App

array::begin() and array::end() in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

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.

array::begin()

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




// 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
array::end()

end() returns an iterator pointing to the past-the-end element in the array container. 
Syntax :  

arrayname.end()
Parameters :
No parameters are passed.

Returns :
This function returns a bidirectional
iterator pointing to the past-the-end element.

Examples:  

Input  : myarray{1, 2, 3, 4, 5};
Output : returns an iterator to the element next to 5 i.e,. some garbage value

Input  : myarray{8, 7};
Output : returns an iterator to the element next to 7 i.e,. some garbage value

Errors and Exceptions
1. It has a no exception throw guarantee. 
2. Shows error when a parameter is passed. 

CPP




// 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;
       
      auto it = myarray.end();
      cout << "\n myarray.end(): " << *it << " [some garbage value]";
    return 0;
}


Output: 

1 2 3 4 5
myarray.end(): 0 [some garbage value]

Let us see the differences in a tabular form -:

  array::begin()  array::end() 
1. It is used to return an iterator pointing to the first element in the array container. It is used to return an iterator pointing to the past-the-end element in the array container.
2. Its syntax is -:
iterator begin(

Its syntax is -:

iterator end()

3. It does not take any parameters. It does not take any parameters.
4. Its complexity is constant. Its iterator validity does not change.
5. Its iterator validity does not change. Its complexity is constant.


Last Updated : 13 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads