C++ STL insert_iterator::operator=, the assignment 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 insert a value into the container and returns the iterator updated to point to the new element in C++ programming

To show: Using the C++ insert_iterator::operator=, the assignment operator to insert a value into the container and returns the iterator updated to point to the new element in C++ programming

 

// C++ STL insert_iterator, operator=, the assignment 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, constructing a list

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

lst.push_back(i);

 

// print the data

cout<<"The lst list data: ";

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

cout<<*lstiter<<" ";

cout<<endl;

 

// using insert_iterator

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

cout<<endl<<"Using insert iterator..."<<endl;

*Iter = 12;

*Iter = 7;

*Iter = 33;

*Iter = 24;

cout<<"Operation: Iter(lst, lst.begin()) then *Iter = 12..."<<endl;

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 lst list data: 10 11 12 13 14 15

Using insert iterator...

Operation: Iter(lst, lst.begin()) then *Iter = 12...

After the insertions, the lst list data: 12 7 33 24 10 11 12 13 14 15

Press any key to continue . . .

 

 

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