Merging won't work if the lists aren't ordered.
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> listObject1, listObject2;
int i;
listObject1.push_back(2);
listObject1.push_back(0);
listObject1.push_back(8);
listObject1.push_back(4);
listObject1.push_back(6);
for(i =1; i <11; i+=2)
listObject2.push_back(i);
cout << "Original contents of listObject1:\n";
list<int>::iterator p = listObject1.begin();
while(p != listObject1.end())
cout << *p++ << " ";
cout << "\n\n";
cout << "Original contents of listObject2:\n";
p = listObject2.begin();
while(p != listObject2.end())
cout << *p++ << " ";
cout << "\n\n";
// this merge will fail
listObject1.merge(listObject2);
cout << "Contents of listObject1 after failed merge:\n";
p = listObject1.begin();
while(p != listObject1.end())
cout << *p++ << " ";
cout << "\n\n";
return 0;
}
Related examples in the same category