Boost logo

Boost :

From: Victor A. Wagner, Jr. (vawjr_at_[hidden])
Date: 2002-07-25 01:15:58


I've noticed in our coding, we sometimes need a comparison operator that
works on just a single element in a struct, instead of the "entire
struct". If we need to sort the container using just this "key"; we, of
course, write the comparison function/functor and then:

std::sort(c.begin(), c.end(), ourfunction);

I decided to write a couple templates and helper functions called (the ones
you'd use) less_element() and greater_element().

if then we have, e.g. :
struct person{
std::string firstname;
std::string lastname:
....other stuff
};

and a container of them, we can:

std::sort(c.begin(), c.end(), less_element(firstname));
or....
std::sort(c.begin(), c.end(), greater_element(lastname));

I'm sorry to be so wordy, but neither of us in this group has ever worked
in a large (>4) staff using C++, so maybe we're just missing a whole lot
(discussion of wisdom of public variables aside...we use them), or these
may be useful to others also.

We'd be happy to clean them up and boostify them if there is interest.
Victor A. Wagner Jr. http://rudbek.com
PGP RSA fingerprint = 4D20 EBF6 0101 B069 3817 8DBF C846 E47A
PGP D-H fingerprint = 98BC 65E3 1A19 43EC 3908 65B9 F755 E6F4 63BB 9D93
The five most dangerous words in the English language:
               "There oughta be a law"


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