C++ STL vector, 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 in C++ programming

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

 

 

 

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

#include <vector>

#include <iostream>

using namespace std;

 

int main(void)

{

unsigned int i;

// vector container

vector <int> vec1;

// vector iterator

vector <int>::iterator vec1_Iter;

// vector reverse iterator

vector <int>::reverse_iterator vec1_rIter;

 

// push data into the vector

vec1.push_back(10);

vec1.push_back(7);

vec1.push_back(3);

 

// print the data and do some operations

cout<<"vec1 vector data: ";

for(i=0; i<vec1.size(); ++i)

cout<<vec1[i]<<' ';

cout<<endl;

cout<<"\nOperation: vec1.begin();"<<endl;

vec1_Iter = vec1.begin();

cout<<"The first element of vec1 vector is "<<*vec1_Iter<<endl;

cout<<"\nOperation: vec1.rbegin();"<<endl;

vec1_rIter = vec1.rbegin();

cout<<"The first element of the reversed vec1 vector is "<<*vec1_rIter<<endl;

return 0;

}

 

Output examples:

 

vec1 vector data: 10 7 3

Operation: vec1.begin();

The first element of vec1 vector is 10

Operation: vec1.rbegin();

The first element of the reversed vec1 vector is 3

Press any key to continue . . .

 

 

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