Boost logo

Boost Users :

From: bringiton bringiton (kneeride_at_[hidden])
Date: 2007-07-23 03:13:43


hi guys, just wondering if you have any shared_ptr tips for sorting vectors

[code]
    std::vector< boost::shared_ptr<int> > v;
    //.. insert items into vector
    std::sort(v.begin(), v.end());
[/code]

OK this doesn't sort by integer. I was hoping shared_ptrs < operator
actually used the < operator of it's template type. however, i guess
it's sorting by pointer value...

the following works OK:

[code]
template <class T>
struct X {
    bool operator()(const boost::shared_ptr<T>& a, const
boost::shared_ptr<T>& b) {
        return *a < *b;
    }
};

int main()
{
    std::vector< boost::shared_ptr<int> > v;
    //.. insert items into vector
    std::sort(v.begin(), v.end(), X<int>());
    return 0;
}
[/code]

Does anyone have a better way of doing this? Maybe boost already
defines a function similar to my X function. Maybe there is a way
without using a predicate...


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net