C++ STL  insert_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++ insert_iterator::operator* to dereference the insert iterator returning the element is addresses in C++ programming

To show: How to use the C++ insert_iterator, operator* to dereference the insert iterator returning the element is addresses in C++ programming

 

// C++ STL insert_iterator, operator*

#include <iterator>

#include <list>

#include <iostream>

using namespace std;

 

int main(void)

{

int i;

// list iterator

list<int>::iterator lstiter;

// list container

list<int> lst;

 

// push/insert data

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

lst.push_back(i);

 

// print the data

cout<<"The original lst list data: ";

for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

cout<<*lstiter<<" ";

cout<<endl;

 

// do some operations

cout<<"\nOperation: Iter(lst, lst.begin()) and *Iter = 21..."<<endl;

insert_iterator< list <int> > Iter(lst, lst.begin());

 

// insert more data

*Iter = 21;

*Iter = 9;

*Iter = 34;

 

cout << "After the insertions, the lst list data: ";

for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

cout<<*lstiter<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

The original lst list data: 10 11 12 13 14 15

Operation: Iter(lst, lst.begin()) and *Iter = 21...

After the insertions, the lst list data: 21 9 34 10 11 12 13 14 15

Press any key to continue . . .

 

 

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