C++ STL vector, rend() and rbegin() code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ rbegin() to return an iterator to the first element in a reversed vector and rend() to return an iterator to the end of a reversed vector in C++ programming

To show: How to use the C++ rbegin() to return an iterator to the first element in a reversed vector and rend() to return an iterator to the end of a reversed vector in C++ programming

 

// C++ STL vector, rend() and rbegin()

#include <vector>

#include <iostream>

using namespace std;

 

int main(void)

{

// vector container

vector <int> vec1;

// vector reverse iterator

vector <int>::reverse_iterator vec1_rIter;

 

// push/insert data

vec1.push_back(7);

vec1.push_back(3);

vec1.push_back(4);

vec1.push_back(1);

 

// print the data

cout<<"Operation: vec1.rbegin() and vec1.rend();"<<endl;

cout<<"vec1 vector data: ";

for(vec1_rIter = vec1.rbegin(); vec1_rIter != vec1.rend(); vec1_rIter++)

cout<<*vec1_rIter<<' ';

cout<<endl;

return 0;

}

 

Output examples:

 

Operation: vec1.rbegin() and vec1.rend();

vec1 vector data: 1 4 3 7

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index