In article <5dd80816-49c4-442d-a25c-076b6c260305
@y21g2000hsf.googlegroups.com>, PeteOlc...@gmail.com says...
[ ... ]
The only reason that I even need to have the contained class is so
that I can overload the operator<() on it, and thus use std::sort().
You can use std::sort without having an overloaded operator< for the
type. You can create the comparison as either a function or a functor,
and then pass it as the third parameter to std::sort. For example:
#include <vector>
#include <algorithm>
#include <iostream>
class X {
int y;
public:
X(int a=0) : y(a) {}
operator int() const { return y; }
};
struct less {
bool operator()(X const &a, X const &b) {
return (int)a < (int)b;
}
};
int main() {
std::vector<X> x;
for (int i=0; i<20; i++)
x.push_back(rand());
std::sort(x.begin(), x.end(), less());
std::copy(x.begin(), x.end(),
std::ostream_iterator<int>(std::cout, "\n"=