Inserting elements from another list at the end : list insert « list « C++ Tutorial






#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 end...
    listIntegers3.insert (listIntegers3.end (),list2.begin (), list2.end ());

    PrintListContents (listIntegers3);

    return 0;
}

void PrintListContents (const list <int>& listInput)
{
    std::list <int>::const_iterator i;
    for ( i = listInput.begin (); i != listInput.end (); ++ i )
        cout << *i << " ";
}








17.8.list insert
17.8.1.Insert one at a time
17.8.2.Insert 3 fours
17.8.3.Insert another list
17.8.4.Insert an array in there
17.8.5.Inserting Elements in the List Using push_front
17.8.6.Inserting Elements in the List Using push_back
17.8.7.Inserting elements from another list at the beginning
17.8.8.Inserting elements from another list at the end
17.8.9.Insert elements of array into a list
17.8.10.Combine insert and begin to add element to the start of a list
17.8.11.Combine insert and end to add elements to the end of a list