STL Lists 2 (program)


//A program to illustrate the list functions

#include <iostream>
#include <list>
using namespace std;

//Function to display the contents of the list
void display(list<int> &list1)
{

    list<int>::const_iterator ptr;

    for (ptr=list1.begin();ptr!=list1.end();ptr++)
    {
        cout<<" "<<*ptr;
    }
}

int main( )

{
list<int> intlist1,intlist2;
const int SIZE=4;
int arr[SIZE]={5,8,4,2};

//adding 4 values to the list (1 2 3 4)
for(int i=1;i<5;i++)
{
intlist1.push_back(i);
}

intlist1.push_front(8);
intlist1.push_back(1);
intlist1.push_back(1);
cout<<endl<<"List 1 :";
display(intlist1);

intlist1.unique( );
cout<<endl<<"List 1 after unique:";
display(intlist1);

intlist2.insert(intlist2.begin( ), arr, arr+SIZE);
cout<<endl<<"List 2 :";
display(intlist2);

intlist1.splice(intlist1.end( ),intlist2);
cout<<endl<<"List 1 after splicing:";
display(intlist1);
cout<<endl<<"List 2 after splicing:";
display(intlist2);

intlist2.insert(intlist2.begin( ), arr, arr+SIZE);
intlist2.sort( );
intlist1.sort( );
cout<<endl<<"List 1 after sorting :";
display(intlist1);
cout<<endl<<"List 2 after sorting :";
display(intlist2);

intlist1.merge(intlist2);
cout<<endl<<"List 1 after merging :";
display(intlist1);
cout<<endl<<"List 2 after merging :";
display(intlist2);

intlist1.unique( );
cout<<endl<<"List 1 after unique :";
display(intlist1);

intlist1.remove(5);
cout<<endl<<"List 1 (5 removed) :";
display(intlist1);

intlist1.swap(intlist2);
cout<<endl<<"List 1 after swapping:";
display(intlist1);
cout<<endl<<"List 2 after swapping:";
display(intlist2);

return 0;
}

The output will be:

List 1 : 8 1 2 3 4 1 1
List 1 after unique : 8 1 2 3 4 1
List 2 : 5 8 4 2
List 1 after splicing : 8 1 2 3 4 1 5 8 4 2
List 2 after splicing :
List 1 after sorting : 1 1 2 2 3 4 4 5 8 8
List 2 after sorting : 2 4 5 8
List 1 after merging : 1 1 2 2 2 3 4 4 4 5 5 8 8 8
List 2 after merging :
List 1 after unique : 1 2 3 4 5 8
List 1 (5 removed) : 1 2 3 4 8
List 1 after swapping :
List 2 after swapping : 1 2 3 4 8

We’ll take a look at the function individually.

intlist1.unique( );

As you can see from the output of the above program, this function is effective only when the values in the list are sorted (otherwise there is a good chance that the same value might still have duplicates in the list).

intlist2.insert(intlist2.begin( ), arr, arr+SIZE);

This is the 3rd form of the insert ( ) function which we discussed earlier. Here ‘arr’ is a pointer (pointer to the first element of the array ‘arr’). Adding SIZE gives the second pointer (which will point to the last element of ‘arr’). Thus the array ‘arr’ will be inserted before the beginning of the list intlist2.

intlist1.splice(intlist1.end( ),intlist2);

Splice will remove the elements of the second container and add them to the first container after the position referred to in the first argument. There are 2 more forms of the splice ( ) function available.

void splice (iterator, 2nd list, iterator-to-element);

This 3 argument form of splice will move a single element to the list container that called the splice function.

void splice (iterator, 2nd list, iterator-to-1st element, iterator-to-2nd element);

The 4-argument form of splice can be used to move a range of elements from the 2nd list.

intlist1.sort( );

This is a member function sort ( ) which is specific for the list container. It takes advantage of the fact that when dealing with lists we don’t need to copy or move the objects (it is sufficient to change the linking pointers). This function arranges the elements in ascending order.

intlist1.merge(intlist2);

The merger( ) function will remove elements from the 2nd list (i.e. the list specified as an argument) and insert them into the first list. For the merge( ) function to work properly both the lists should be sorted (the results otherwise will be ambiguous).

intlist1.remove(5);

The remove( ) function removes all occurrences of the specified element from the list.

Beware: remove( ) will eliminate all occurrences of the element (not just the first occurrence but all).

intlist1.swap(intlist2);

The swap( ) function swaps the specified lists.


Go to the section on Deque

Go back to Contents page 2.


Copyright © 2004 Sethu Subramanian All rights reserved.