C++ STL list, constructors program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

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 list constructor to construct a list of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other list in C++ programming

To show: How to use the C++ list, constructor to construct a list of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other list in C++ programming

 

// C++ STL list constructors

#include <list>

#include <iostream>

using namespace std;

 

int main(void)

{

// list iterators

list <int>::iterator li0Iter, li1Iter, li2Iter, li3Iter, li4Iter, li5Iter, li6Iter;

// create an empty list li0

list <int> li0;

// create a list li1 with 10 elements of default value 0

list <int> li1(10);

// create a list li2 with 8 elements of value 7

list <int> li2(8, 7);

// create a list li3 with 9 elements of value 8 and with the allocator of list li2

list <int> li3(9, 8, li2.get_allocator());

// li4, a copy of list li2

list <int> li4(li2);

// create a list li5 by copying the range of li4[_First, _Last)

li4Iter = li4.begin();

li4Iter++;

li4Iter++;

li4Iter++;

li4Iter++;

list <int> li5(li4.begin(), li4Iter);

// create a list li6 by copying the range of li4[_First, _Last) and with the allocator of list li2

li4Iter = li4.begin();

li4Iter++;

li4Iter++;

li4Iter++;

list <int> li6(li4.begin(), li4Iter, li2.get_allocator());

 

// do some operations

cout<<"Operation: list <int> li0;"<<endl;

cout<<"li0 list data: ";

for(li0Iter = li0.begin(); li0Iter != li0.end(); li0Iter++)

cout<<" "<<*li0Iter;

cout<<endl;

cout<<"\nOperation: list <int> li1(10);"<<endl;

cout<<"li1 list data: ";

for(li1Iter = li1.begin(); li1Iter != li1.end(); li1Iter++)

cout<<" "<<*li1Iter;

cout<<endl;

cout<<"\nOperation: list <int> li2(8, 7);"<<endl;

cout<<"li2 list data: ";

for(li2Iter = li2.begin(); li2Iter != li2.end(); li2Iter++)

cout<<" "<<*li2Iter;

cout<<endl;

cout<<"\nOperation: list <int> li3(9, 8, li2.get_allocator());"<<endl;

cout<<"li3 list data: ";

for(li3Iter = li3.begin(); li3Iter != li3.end(); li3Iter++)

cout<<" "<<*li3Iter;

cout<<endl;

cout<<"\nOperation: list <int> li4(li2);"<<endl;

cout<<"li4 list data: ";

for(li4Iter = li4.begin(); li4Iter != li4.end(); li4Iter++)

cout<<" "<<*li4Iter;

cout<<endl;

cout<<"\nOperation1: li4Iter = li4.begin(), li4Iter++..."<<endl;

cout<<"Operation2: list <int> li5(li4.begin(), li4Iter);"<<endl;

cout<<"li5 list data: ";

for(li5Iter = li5.begin(); li5Iter != li5.end(); li5Iter++)

cout<<" "<<*li5Iter;

cout<<endl;

cout<<"\nOperation1: li4Iter = li4.begin(), li4Iter++..."<<endl;

cout<<"Operation2: list <int> li6(li4.begin(), li4Iter, li2.get_allocator());"<<endl;

cout<<"li6 list data: ";

for(li6Iter = li6.begin(); li6Iter != li6.end(); li6Iter++)

cout<<" "<<*li6Iter;

cout<<endl;

return 0;

}

 

Output examples:

 

Operation: list <int> li0;

li0 list data:

Operation: list <int> li1(10);

li1 list data: 0 0 0 0 0 0 0 0 0 0

Operation: list <int> li2(8, 7);

li2 list data: 7 7 7 7 7 7 7 7

Operation: list <int> li3(9, 8, li2.get_allocator());

li3 list data: 8 8 8 8 8 8 8 8 8

Operation: list <int> li4(li2);

li4 list data: 7 7 7 7 7 7 7 7

Operation1: li4Iter = li4.begin(), li4Iter++...

Operation2: list <int> li5(li4.begin(), li4Iter);

li5 list data: 7 7 7 7

Operation1: li4Iter = li4.begin(), li4Iter++...

Operation2: list <int> li6(li4.begin(), li4Iter, li2.get_allocator());

li6 list data: 7 7 7

Press any key to continue . . .

 

 

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