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...
On 23/07/07, bringiton bringiton
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...
A shared_ptr is still a pointer, so the principle of least surprise means that it should sort like a pointer.
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...
This is one of my all-time favourite Boost.Lambda examples: std::sort( v.begin(), v.end(), *_1 < *_2 ); ~ Scott
participants (2)
-
bringiton bringiton
-
Scott McMurray