Demonstrate merge() in list
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> listObject1, listObject2;
int i;
for(i = 0; i <10; i+=2)
listObject1.push_back(i);
for(i =1; i <11; i+=2)
listObject2.push_back(i);
cout << "Original size of listObject1: ";
cout << listObject1.size() << endl;
cout << "Original contents of listObject1:\n";
list<int>::iterator p = listObject1.begin();
while(p != listObject1.end())
cout << *p++ << " ";
cout << "\n\n";
cout << "Original size of listObject2: ";
cout << listObject2.size() << endl;
cout << "Original contents of listObject2:\n";
p = listObject2.begin();
while(p != listObject2.end())
cout << *p++ << " ";
cout << "\n\n";
// merge the two lists
listObject1.merge(listObject2);
cout << "Size of listObject1 after merge: ";
cout << listObject1.size() << endl;
cout << "Merged contents of listObject1:\n";
p = listObject1.begin();
while(p != listObject1.end())
cout << *p++ << " ";
cout << "\n\n";
cout << "listObject2 is now empty, its size is ";
cout << listObject2.size() << endl;
return 0;
}
Related examples in the same category