C++ STL insert_iterator::operator++, the increment 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++ insert_iterator::operator++ to increment the insert_iterator to the next location into which a value may be stored in C++ programming

To show: How to use the insert_iterator::operator++, the increment operator to increment the insert_iterator to the next location into which a value may be stored in C++ programming

 

// C++ STL insert_iterator, operator++, the increment operator

#include <iterator>

#include <vector>

#include <iostream>

using namespace std;

 

int main(void)

{

int i;

// vector container

vector<int> vec;

 

// push/insert data into vector container

for(i = 10; i<=15; ++i)

vec.push_back(i);

 

// vector iterator

vector <int>::iterator veciter;

 

// print the data

cout<<"The vec vector data: ";

// iterate all the elements and print

for(veciter = vec.begin(); veciter != vec.end(); veciter++)

cout<<*veciter<<" ";

cout<<endl;

 

// do some operation

cout<<"\nOperation: j(vec, vec.begin()) then *j = 17 and j++..."<<endl;

insert_iterator<vector<int> > j(vec, vec.begin());

*j = 17;

j++;

*j = 9;

 

cout<<"After the insertions, the vec vector data: ";

for(veciter = vec.begin(); veciter != vec.end(); veciter++)

cout<<*veciter<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

The vec vector data: 10 11 12 13 14 15

Operation: j(vec, vec.begin()) then *j = 17 and j++...

After the insertions, the vec vector data: 17 9 10 11 12 13 14 15

Press any key to continue . . .

 

 

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