Boost logo

Boost :

From: jsiek_at_[hidden]
Date: 2000-01-15 03:32:22


Hi Jim,

I'm afraid there is already a good solution to the problem you
describe, and it is by one of our members here at boost, Nicolai
Jusuttis. With his compose library, and with the member function
functor from the standard, you can do the same things that your
library does. Here's the example you gave rewritten using the compose
library.

Ciao,

Jeremy

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <boost/compose.hpp>

using namespace std;
using namespace boost;

class Person
{
public:
  Person(const string& n) : _name(n) { }
  const string& GetName ( ) const { return _name; }
protected:
  string _name;
};

typedef vector < Person* > PersonVector ;
typedef PersonVector::iterator PersonVecIter ;
typedef PersonVector::const_iterator PersonVecConstIter ;

int
main(int,char*[])
{
  const char* names[] = { "fred", "walter", "joe", "suzy", "jim" };
  PersonVector aPersonVec;
  for (int i = 0; i < sizeof(names)/sizeof(const char*); ++i)
    aPersonVec.push_back(new Person(names[i]));

  // sort the people by name
  sort(aPersonVec.begin(), aPersonVec.end(),
       compose_f_gx_hy(less<string>(),
                       mem_fun(Person::GetName),
                       mem_fun(Person::GetName)));

  // print out the names of the people
  transform(aPersonVec.begin(), aPersonVec.end(),
            ostream_iterator<string>(cout, " "),
            mem_fun(Person::GetName));
  cout << endl;

  for (int i = 0; i < aPersonVec.size(); ++i)
    delete aPersonVec[i];

  return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk