×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

C++ STL Vector Iterators with Example

C++ STL Vector Iterators: Here, we are going to learn about the various vector iterators in C++ STL with Example. By IncludeHelp Last updated : December 11, 2023

C++ STL Vector Iterators

Following are the iterators (public member functions) in C++ STL which can be used to access the elements,

Iterator (public member function) Description
vector::begin() It returns an iterator pointing to the beginning (first) element of the vector.
vector::end() It returns an iterator pointing to the past-the-element of the vector.
vector::rbegin() It returns a reverse iterator pointing to the reverse beginning element of the vector
vector::rend() It returns a reverse iterator pointing to the element preceding the first element of the vector (known as reverse ending).
vector::cbegin() It returns a const iterator pointing to the beginning element of the vector.
vector::cend() It returns a const iterator pointing to the past-the-last element of the vector.
vector::crbegin() It returns a const reverse iterator pointing to the reverse beginning element of the vector.
vector::crend() It returns a const reverse iterator pointing to the element preceding the first element of the vector (from reverse ending).

C++ STL Vector Iterators Example

C++ STL program to demonstrate example of vector iterators.

//C++ STL program to demonstrate example of 
//vector iterators 

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;

    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);

    //begin() and end() function
    vector<int>::iterator it;
    it = v1.begin();
    cout << "first element is: " << *it << endl;
    it = v1.end()-1;
    cout << "last element is: " << *it << endl;
    
    //rbegin() and rend() function
    vector<int>::reverse_iterator rit;
    rit = v1.rbegin();
    cout << "first element (from reverse) is: " << *rit << endl;
    rit = v1.rend()-1;
    cout << "last element  (from reverse) is: " << *rit << endl;    

    //cbegin() and cend() function
    vector<int>::const_iterator cit;
    cit = v1.cbegin();
    cout << "first element is: " << *cit << endl;
    cit = v1.cend()-1;
    cout << "last element is: " << *cit << endl;    

    //crbegin() and crend() function
    vector<int>::const_reverse_iterator crit;
    crit = v1.crbegin();
    cout << "first element (from reverse) is: " << *crit << endl;
    crit = v1.crend()-1;
    cout << "last element  (from reverse) is: " << *crit << endl;    
    
    return 0;
}

Output

first element is: 10    
last element is: 50
first element (from reverse) is: 50    
last element  (from reverse) is: 10    
first element is: 10    
last element is: 50
first element (from reverse) is: 50    
last element  (from reverse) is: 10    

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.