
I can't figure out how to use "sort" method in ptr_vector with the function object. Operator < should be defined, but for various reasons I cant define it in the class A, and I have to do the comparison between the A values using some other values. Example: class A { public: int i; }; class Compare { public: Compare (int i) : _i(i){} //this does not compile, but what's the proper way to define // function object for the sort method in ptr_vector? bool operator<(const A& v1, const A& v2) { return (v1.i > v2.i) &&(_i != 1); } private: int _i; }; ptr_vector<A> lst; Compare cmp(3); lst.sort(cmp);

archie14 <admin <at> tradeplatform.us> writes:
I can't figure out how to use "sort" method in ptr_vector with the function object. Operator < should be defined, but for various reasons I cant define
it
in the class A, and I have to do the comparison between the A values using some other values.
... I "solved" the problem by declaring static comparison method and static member variable to use inside comparison method. Result works, but it is ugly. Question remains - how to define a function object without falling to the static member usage?

2010/8/5 archie14 <admin@tradeplatform.us>
class Compare { public: Compare (int i) : _i(i){} //this does not compile, but what's the proper way to define // function object for the sort method in ptr_vector? bool operator<(const A& v1, const A& v2) { return (v1.i > v2.i) &&(_i != 1); } private: int _i; };
Try making your operator const: bool operator<(const A& v1, const A& v2) *const* Roman Perepelitsa.

Roman Perepelitsa <roman.perepelitsa <at> gmail.com> writes:
class Compare { public: Compare (int i) : _i(i){} //this does not compile, but what's the proper way to define // function object for the sort method in ptr_vector? bool operator<(const A& v1, const A& v2) { return (v1.i > v2.i) &&(_i != 1); } ... };
Try making your operator const: bool operator<(const A& v1, const A& v2) const
Roman Perepelitsa.
No, it did not work. Complains that binary 'operator <' has too many parameters

2010/8/5 archie14 <admin@tradeplatform.us>
Roman Perepelitsa <roman.perepelitsa <at> gmail.com> writes:
class Compare { public: Compare (int i) : _i(i){} //this does not compile, but what's the proper way to define // function object for the sort method in ptr_vector? bool operator<(const A& v1, const A& v2) { return (v1.i > v2.i) &&(_i != 1); } ... };
Try making your operator const: bool operator<(const A& v1, const A& v2) const
Roman Perepelitsa.
No, it did not work. Complains that
binary 'operator <' has too many parameters
Can you post your exact code and exact error message? Roman Perepelitsa.

archie14 skrev:
Roman Perepelitsa <roman.perepelitsa <at> gmail.com> writes:
class Compare { public: Compare (int i) : _i(i){} //this does not compile, but what's the proper way to define // function object for the sort method in ptr_vector? bool operator<(const A& v1, const A& v2) { return (v1.i > v2.i) &&(_i != 1); } ... }; Try making your operator const: bool operator<(const A& v1, const A& v2) const
Roman Perepelitsa.
No, it did not work. Complains that
binary 'operator <' has too many parameters
Indeed, when defining the comparison operator as a member you need to supply only one argument. HTH -Thorsten

On 8/4/2010 8:12 PM, archie14 wrote:
I can't figure out how to use "sort" method in ptr_vector with the function object. Operator< should be defined, but for various reasons I cant define it in the class A, and I have to do the comparison between the A values using some other values.
Example:
class A { public: int i; };
class Compare { public: Compare (int i) : _i(i){} //this does not compile, but what's the proper way to define // function object for the sort method in ptr_vector? bool operator<(const A& v1, const A& v2) { return (v1.i> v2.i)&&(_i != 1); } private: int _i; };
ptr_vector<A> lst; Compare cmp(3); lst.sort(cmp);
The following works for me: #include <iostream> #include <functional> #include <boost/ptr_container/ptr_vector.hpp> struct A { A(int i) : _i(i) { } int _i; }; struct Compare : std::binary_function<A,A,bool> { Compare(int i) : _i(i) { } bool operator()(const A& v1, const A& v2) const { return (v1._i < v2._i) && (_i != 1); } int _i; }; int main() { boost::ptr_vector<A> lst; lst.push_back(new A(3)); lst.push_back(new A(2)); lst.push_back(new A(1)); Compare cmp(3); lst.sort(cmp); boost::ptr_vector<A>::iterator a = lst.begin(); while (a != lst.end()) { std::cout << a++->_i << std::endl; } }
participants (4)
-
archie14
-
Roman Perepelitsa
-
Thorsten Ottosen
-
Tim Moore