C++ STL iterator, operator<= program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
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++ iterator, operator<= to test if the iterator object on the left side of the operator is less than or equal to the iterator object on the right side in C++ programming
To show: How to use the C++ iterator, operator<= to test if the iterator object on the left side of the operator is less than or equal to the iterator object on the right side in C++ programming
// C++ STL iterator, operator<=
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main(void)
{
int i;
// vector container
vector<int> vec;
// vector iterator
vector <int>::iterator veciter;
// push the data
for(i = 10; i<= 15; ++i)
vec.push_back(i);
// print the data
cout<<"The vec vector data: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
vector <int>::reverse_iterator rvecpos1 = vec.rbegin()+1, rvecpos2 = vec.rbegin();
cout<<"The iterator rvecpos1 points to the second element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"The iterator rvecpos2 points to the first element in the reversed sequence: "<<*rvecpos2<<endl;
cout<<"\nOperation: rvecpos1<=rvecpos2"<<endl;
if(rvecpos1<=rvecpos2)
cout<<"The iterator rvecpos1 is less than or equal to the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is greater than the iterator rvecpos2."<<endl;
cout<<"\nOperation: rvecpos2++"<<endl;
rvecpos2++;
cout<<"The iterator rvecpos2 now points to the second element in the reversed sequence: "<<*rvecpos2<<endl;
cout<<"\nOperation: rvecpos1 <= rvecpos2"<<endl;
if(rvecpos1 <= rvecpos2)
cout<<"The iterator rvecpos1 is less than or equal to the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is greater than the iterator rvecpos2."<<endl;
return 0;
}
Output examples:
The vec vector data: 10 11 12 13 14 15
The iterator rvecpos1 points to the second element in the reversed sequence: 14
The iterator rvecpos2 points to the first element in the reversed sequence: 15
Operation: rvecpos1<=rvecpos2
The iterator rvecpos1 is greater than the iterator rvecpos2.
Operation: rvecpos2++
The iterator rvecpos2 now points to the second element in the reversed sequence: 14
Operation: rvecpos1 <= rvecpos2
The iterator rvecpos1 is less than or equal to the iterator rvecpos2.
Press any key to continue . . .