C++ STL list, splice() code 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 C++ splice() to remove elements from the argument list and inserts them into the target list in C++ programming

To show: How to use the C++ list, splice() to remove elements from the argument list and inserts them into the target list in C++ programming

 

// C++ STL list, splice()

#include <list>

#include <iostream>

using namespace std;

 

int main(void)

{

// list containers

list <int> ls1, ls2, ls3, ls4;

// list iterators

list <int>::iterator ls1Iter, ls2Iter, ls3Iter, ls4Iter, PIter, QIter, RIter;

 

// insert/push data into list

ls1.push_back(7);

ls1.push_back(15);

ls2.push_back(9);

ls2.push_back(22);

ls2.push_back(12);

ls3.push_back(29);

ls3.push_back(30);

ls4.push_back(33);

ls4.push_back(25);

ls4.push_back(51);

 

// print the data and do some operations

cout<<"ls1 list data: ";

for(ls1Iter = ls1.begin(); ls1Iter != ls1.end(); ls1Iter++)

cout<<" "<<*ls1Iter;

cout<<endl;

cout<<"ls2 list data: ";

for(ls2Iter = ls2.begin(); ls2Iter != ls2.end(); ls2Iter++)

cout<<" "<<*ls2Iter;

cout<<endl;

cout<<"ls3 list data: ";

for(ls3Iter = ls3.begin(); ls3Iter != ls3.end(); ls3Iter++)

cout<<" "<<*ls3Iter;

cout<<endl;

cout<<"ls4 list data: ";

for(ls4Iter = ls4.begin(); ls4Iter != ls4.end(); ls4Iter++)

cout<<" "<<*ls4Iter;

cout<<endl;

cout<<"\nOperation: ls2.splice(PIter, ls1);"<<endl;

PIter = ls2.begin();

PIter++;

ls2.splice(PIter, ls1);

cout<<"ls2 list data, after splicing ls1 list into ls2 list: ";

for(ls2Iter = ls2.begin(); ls2Iter != ls2.end(); ls2Iter++)

cout<<" "<<*ls2Iter;

cout<<endl;

cout<<"\nOperation: ls2.splice(PIter, ls3, QIter);"<<endl;

QIter = ls3.begin();

ls2.splice(PIter, ls3, QIter);

cout<<"ls2 list data, after splicing the first element of ls3 list into ls2 list: ";

for(ls2Iter = ls2.begin(); ls2Iter != ls2.end(); ls2Iter++)

cout<<" "<<*ls2Iter;

cout<<endl;

cout<<"\nOperation: ls2.splice(PIter, ls4, QIter, RIter);"<<endl;

QIter = ls4.begin();

RIter = ls4.end();

RIter--;

ls2.splice(PIter, ls4, QIter, RIter);

cout<<"ls2 list data, after splicing a range of ls4 list into ls2 list: ";

for(ls2Iter = ls2.begin(); ls2Iter != ls2.end(); ls2Iter++)

cout<<" "<<*ls2Iter;

cout<<endl;

return 0;

}

 

Output examples:

 

ls1 list data: 7 15

ls2 list data: 9 22 12

ls3 list data: 29 30

ls4 list data: 33 25 51

Operation: ls2.splice(PIter, ls1);

ls2 list data, after splicing ls1 list into ls2 list: 9 7 15 22 12

Operation: ls2.splice(PIter, ls3, QIter);

ls2 list data, after splicing the first element of ls3 list into ls2 list: 9 7 15 29 22 12

Operation: ls2.splice(PIter, ls4, QIter, RIter);

ls2 list data, after splicing a range of ls4 list into ls2 list: 9 7 15 29 33 25 22 12

Press any key to continue . . .

 

 

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