C++ STL  insert_iterator::reference 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++ reference which is a type that provides a reference to an element in a sequence controlled by the associated container in C++ programming

To show: How to use the C++ insert_iterator, reference which is a type that provides a reference to an element in a sequence controlled by the associated container in C++ programming

 

// C++ STL insert_iterator, reference type

#include <iterator>

#include <list>

#include <iostream>

using namespace std;

 

int main(void)

{

// list container

list<int> lst;

// list iterator

list<int>::iterator lstIter;

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

 

// push/insert data

*iivIter = 12;

*iivIter = 21;

*iivIter = 9;

*iivIter = 31;

 

// print the data

cout<<"The lst list data: ";

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

cout<<*lstIter<<" ";

cout<<endl;

 

cout<<"\nOperation: refirst = *(lst.begin())\n";

insert_iterator<list<int> >::reference refirst = *(lst.begin());

cout<<"Then, the first element in the lst list is: "<<refirst<<endl;

return 0;

}

 

Output examples:

 

The lst list data: 12 21 9 31

Operation: refirst = *(lst.begin())

Then, the first element in the lst list is: 12

Press any key to continue . . .

 

 

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