#include <iostream>
#include <list>
using namespace std;
typedef list<int> intList;
typedef list<int>::iterator intListItor;
template<class T, class A>
void showList(const list<T, A>& aList);
int main()
{
// define a list of integers with 5 elements
intList ListA(5);
int j = 0;
for (intListItor ia = ListA.begin(); ia != ListA.end(); ++ia)
*ia = 5 * j++;
cout << "ListA" << "\n";
showList(ListA);
// remove an element from ListA
ListA.remove(5);
cout << "5 removed from ListA:\n";
showList(ListA);
// define a list of integers with 6 elements
intList ListB(6);
j = 10;
for (intListItor ib = ListB.begin(); ib != ListB.end(); ++ib)
*ib = 2 * j--;
cout << "ListB" << "\n";
showList(ListB);
// splice ListA & ListB and add an element 10 at the beginning of ListA
cout << "splice ListA & ListB and add 10 to the beginning:\n";
ListA.splice(++(++ListA.begin()), ListB);
ListA.push_front(10);
cout << "ListA" << "\n";
showList(ListA);
// sort ListA first
ListA.sort(); // so that elements with same value will group together
cout << "ListA sorted:\n";
showList(ListA);
// remove duplicates from ListA
ListA.unique();
cout << "Duplicates in ListA removed:\n";
showList(ListA);
return 0;
}
//
// Display list elements
//
template<class T, class A>
void showList(const list<T, A>& aList)
{
cout << "size() = " << aList.size() << ":\t";
for (list<T, A>::const_iterator i = aList.begin(); i != aList.end(); ++i)
cout << *i << ", ";
cout << "\n\n";
}