Mega Code Archive

 
Categories / C++ / Data Structure
 

Merging One List with Another

#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 << "Contents of listObject1:\n";   list<int>::iterator p = listObject1.begin();   while(p != listObject1.end()) {     cout << *p << " ";     p++;   }   cout << endl << endl;   cout << "Contents of listObject2:\n";   p = listObject2.begin();  while(p != listObject2.end()) {     cout << *p << " ";     p++;   }   cout << endl << endl;   // now, merge the two lists   listObject1.merge(listObject2);   if(listObject2.empty())     cout << "listObject2 is now empty\n";   cout << "Contents of listObject1 after merge:\n";   p = listObject1.begin();   while(p != listObject1.end()) {     cout << *p << " ";     p++;   }   return 0; }