ListOps.cc


#include <iostream>                 // for cout, endl
#include <string>
#include <list>       
using namespace std;

void print( list<string>& );

int main()
{
    list<string> animals;                                         //(A)       

    animals.push_back( "cheetah" );                               //(B)
    animals.push_back( "lion" );                                  //(C)
    animals.push_back( "cat" );                                   //(D)
    animals.push_back( "fox" );                                   //(E)
    animals.push_back( "elephant" );                              //(F)
    animals.push_back( "cat" );     // duplicate cat              //(G)

    print( animals );               // cheetah lion cat fox 
                                    // elephant cat
 
    animals.pop_back();                                           //(H)
    print( animals );               // cheetah lion cat fox 
                                    // elephant
 
    animals.remove( "lion" );       // first occurrence of lion   //(I)
    print( animals );               // cheetah cat fox elephant    
  
    animals.push_front( "lion" );                                 //(J)
    print( animals );          // lion cheetah cat fox elephant
    animals.pop_front( );                                         //(K)
    print( animals );               // cheetah cat fox elephant
  
    animals.insert( animals.end(), "cat" );                       //(L)
    print( animals );          // cheetah cat fox elephant cat
  
    animals.sort();                                               //(M)
    print( animals );          // cat cat cheetah elephant fox   
  
    animals.unique();                                             //(N)
    print( animals );               // cat cheetah elephant fox
 
    //another list needed for demonstrating splicing and merging:
    list<string> pets;                                            //(O)
    pets.push_back( "cat" );
    pets.push_back( "dog" );
    pets.push_back( "turtle" );
    pets.push_back( "bird" );

    animals.splice( animals.begin(), pets, pets.begin() );        //(P)
    print( animals );          // cat cat cheetah elephant fox 
    print( pets );                  // dog turtle bird
 
    pets.sort();                    // bird dog turtle            //(Q)
 
    animals.merge( pets );                                        //(R)

    cout << pets.empty() << endl;   // true                       //(S)

    print( animals );               // bird cat cat cheetah       //(T)
                                    // dog elephant fox 
                                    // turtle
    return 0;
}

void print( list<string>& li ) {                                  //(U)
    typedef list<string>::const_iterator CI;              
    cout << "The number of items in the list: " 
         <<  li.size() << endl;;
    for ( CI iter = li.begin(); iter != li.end(); iter++ )
        cout << *iter << " ";                               
    cout << endl << endl;
}


Results

C:\classes\ece538\work\kak05>ListOps
The number of items in the list: 6
cheetah lion cat fox elephant cat

The number of items in the list: 5
cheetah lion cat fox elephant

The number of items in the list: 4
cheetah cat fox elephant

The number of items in the list: 5
lion cheetah cat fox elephant

The number of items in the list: 4
cheetah cat fox elephant

The number of items in the list: 5
cheetah cat fox elephant cat

The number of items in the list: 5
cat cat cheetah elephant fox

The number of items in the list: 4
cat cheetah elephant fox

The number of items in the list: 5
cat cat cheetah elephant fox

The number of items in the list: 3
dog turtle bird

1
The number of items in the list: 8
bird cat cat cheetah dog elephant fox turtle


Maintained by John Loomis, updated Sun Jan 07 12:05:57 2007