mem_fun_ref and print : list display « List « C++






mem_fun_ref and print

  
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>

using namespace std;

class Employee
{
   public:
   Employee( const string& name = "", int sales = 0,int district = 0 );
   bool operator>( const Employee& rhs ) const;
   void print() const;

   private:
   int district_;
   string name_;
   int sales_;
};

inline
Employee::Employee( const string& name, int sales,
   int district )
   : district_( district ), name_( name ), sales_( sales )
{} 

inline
bool Employee::operator>( const Employee& rhs ) const
{ return sales_ > rhs.sales_; }

inline
void Employee::print() const
{ cout << name_ << " from District " << district_
   << " has sales of $" << sales_ << endl;
}

int main( )
{
   list<Employee> list1;
   list1.push_back( Employee( "A", 3, 1 ) );
   list1.push_back( Employee( "B", 4, 1 ) );
   list1.push_back( Employee( "C", 8, 1 ) );

   list1.sort( greater<Employee>() );
   for_each( list1.begin(), list1.end(), mem_fun_ref( &Employee::print ) );

}
  
    
  








Related examples in the same category

1.Print out each element in a list
2.Print all elements in a list with copy function
3.Use ostream_iterator and copy to display collection content