Home » C++ STL

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.
Submitted by IncludeHelp, on May 11, 2019

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 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    





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.