// List_Item_Process.cpp : Defines the entry point for the console application.
//
#include <iostream>
using std::cout;
using std::endl;
#include <list>
#include <algorithm>
#include <iterator>
// prototype for function template printLIst
template < class T >
void printList( const std::list< T > & listRef );
int _tmain(int argc, _TCHAR* argv[])
{
const int SIZE = 4;
int iArray[ SIZE ] = { 2, 6, 4, 8 };
std::list< int > values;
std::list< int > otherValues;
//insert items in values
values.push_front( 1 );
values.push_front( 2 );
values.push_back( 4 );
values.push_back( 3 );
cout<< "values contains: ";
printList( values );
values.sort();
cout<< "/nvalues after sorting containers: ";
printList( values );
otherValues.insert( otherValues.begin(), iArray, iArray + SIZE );
cout<< "/nAfter insert, otherValues contains: ";
printList( otherValues );
auto it = otherValues.begin();
for( ; it!= otherValues.end(); ++it)
{
if ( 4 == *it )
break;
}
values.splice( values.end(), otherValues );
//values.splice( values.end(), otherValues, it, otherValues.end() );
cout<<"/nAfter splice, values contains: ";
printList( values );
values.sort();
cout<<"/nAfter sort, values contains: ";
printList( values );
otherValues.insert( otherValues.begin(), iArray, iArray + SIZE );
otherValues.sort();
cout<<"/nAfter insert, otherValues contains: ";
printList( otherValues );
values.merge( otherValues );
cout<<"/nAfter merge: /n values contains: ";
printList( otherValues );
values.pop_front();
values.pop_back();
cout<<"/nAfter pop_front and pop_back:"
<<"/n values contains: ";
printList( values );
values.unique();
cout<< "/nAfter unique, values contains: ";
printList( values );
values.swap( otherValues );
cout<< "/nAfter swap:/n values contains: ";
printList( values );
cout<<"/n otherValues contains: ";
printList( otherValues );
values.assign( otherValues.begin(), otherValues.end() );
cout<< "/nAfter assign, values contains: ";
printList( values );
values.merge( otherValues );
cout<<"/nAfter merge, values contains: ";
printList( values );
values.remove( 4 );
cout<<"/nAfter remove( 4 ), values contains: ";
printList( values );
cout<<endl;
return 0;
}
template< class T >
void printList( const std::list<T>& listRef )
{
if( listRef.empty() )
{
cout<<"List is empty";
}
else
{
std::ostream_iterator<T> outputIt( cout, " ");
std::copy( listRef.begin(), listRef.end(), outputIt );
}
}