Mega Code Archive

 
Categories / C++ Tutorial / List
 

Inserting elements from another list at the beginning

#include <list> #include <iostream> using namespace std; void PrintListContents (const list <int>& listInput); int main () {     list <int> list1;     list1.insert (list1.begin (), 4);     list1.insert (list1.begin (), 3);     list1.insert (list1.begin (), 2);     list1.insert (list1.begin (), 1);     list1.insert (list1.end (), 5);     PrintListContents (list1);     list <int> list2;     list2.insert (list2.begin (), 4, 0);     cout << list2.size () << "' elements of a value:" << endl;     PrintListContents (list2);     list <int> listIntegers3;     // Inserting elements from another list at the beginning...     listIntegers3.insert (listIntegers3.begin (),list1.begin (), list1.end ());     return 0; } void PrintListContents (const list <int>& listInput) {     std::list <int>::const_iterator i;     for ( i = listInput.begin (); i != listInput.end (); ++ i )         cout << *i << endl; }