I've had this same pattern come up 4 or 5 times now, and each time I've
been unable to find a stl + boost solution that allowed me to do what I
want without writing my own less function. Basically, I want something
that will let me do a sort, or define a map on structs or pairs without
writing a less that compares the member variable or .first. My last
attempt:
vector
Chris Jurney wrote:
I've had this same pattern come up 4 or 5 times now, and each time I've been unable to find a stl + boost solution that allowed me to do what I want without writing my own less function. Basically, I want something that will let me do a sort, or define a map on structs or pairs without writing a less that compares the member variable or .first. <snip> No such luck. It looks like I can do operator * and & on lambda objects, but not access members through them. Is there anything in boost, stl, or elsewhere that lets me not write this:
class myLess
{
bool operator() (pair
a, pair b) {return a.first < b.first;}
}
Yes: struct X { int a; }; std::vector<X> v; With boost.bind: std::sort( v.begin() , v.end() , boost::bind( std::less<int>() , boost::bind(&X::a, _1) , boost::bind(&X::a, _2) ) ); and with boost.lambda: std::sort( v.begin() , v.end() , bind(&X::a, _1) < bind(&X::a, _2) ); HTH, -- Daniel Wallin
--- At Fri, 26 Mar 2004 00:48:49 +0100, Daniel Wallin wrote:
Chris Jurney wrote:
I've had this same pattern come up 4 or 5 times now, and each time I've been unable to find a stl + boost solution that allowed me to do what I want without writing my own less function. Basically, I want something that will let me do a sort, or define a map on structs or pairs without writing a less that compares the member variable or .first. <snip> No such luck. It looks like I can do operator * and & on lambda objects, but not access members through them. Is there anything in boost, stl, or elsewhere that lets me not write this:
class myLess
{
bool operator() (pair
a, pair b) {return a.first < b.first;}
}
Yes:
struct X { int a; };
std::vector<X> v;
With boost.bind:
std::sort( v.begin() , v.end() , boost::bind( std::less<int>() , boost::bind(&X::a, _1) , boost::bind(&X::a, _2) ) );
I don't know lambda, so I won't comment on that. But, I thought that the first parameter to bind was a function pointer? You can bind to an address? Please explain how this works?
and with boost.lambda:
std::sort( v.begin() , v.end() , bind(&X::a, _1) < bind(&X::a, _2) );
...Duane
Duane Murphy wrote:
--- At Fri, 26 Mar 2004 00:48:49 +0100, Daniel Wallin wrote:
struct X { int a; };
std::vector<X> v;
With boost.bind:
std::sort( v.begin() , v.end() , boost::bind( std::less<int>() , boost::bind(&X::a, _1) , boost::bind(&X::a, _2) ) );
I don't know lambda, so I won't comment on that. But, I thought that the first parameter to bind was a function pointer? You can bind to an address?
Please explain how this works?
&X::a is not an address. It is a member pointer. boost::bind allows you to work with pointers to members as if they were function objects. It does this by automatically translating &X::a to boost::mem_fn(&X::a).
why do you want to sort ONLY on the 1st element of the pair? At Thursday 2004-03-25 11:34, you wrote:
content-class: urn:content-classes:message Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C41297.D800D40A"
I've had this same pattern come up 4 or 5 times now, and each time I've been unable to find a stl + boost solution that allowed me to do what I want without writing my own less function. Basically, I want something that will let me do a sort, or define a map on structs or pairs without writing a less that compares the member variable or ..first. My last attempt:
vector
> myVector std::sort(myVector.begin(), myVector.end(), _1.first < _2.first); No such luck. It looks like I can do operator * and & on lambda objects, but not access members through them. Is there anything in boost, stl, or elsewhere that lets me not write this:
class myLess { bool operator() (pair
a, pair b) {return a.first < b.first;} } Thanks for any help, Chris
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Victor A. Wagner Jr. http://rudbek.com The five most dangerous words in the English language: "There oughta be a law"
I can't speak for the original poster, but I often have a collection of pairs of the form (time, data) and want to put the whole thing in chronological order. Victor A. Wagner Jr. wrote:
why do you want to sort ONLY on the 1st element of the pair?
At Thursday 2004-03-25 11:34, you wrote:
content-class: urn:content-classes:message Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C41297.D800D40A"
I've had this same pattern come up 4 or 5 times now, and each time I've been unable to find a stl + boost solution that allowed me to do what I want without writing my own less function. Basically, I want something that will let me do a sort, or define a map on structs or pairs without writing a less that compares the member variable or ..first. My last attempt:
vector
> myVector std::sort(myVector.begin(), myVector.end(), _1.first < _2.first); No such luck. It looks like I can do operator * and & on lambda objects, but not access members through them. Is there anything in boost, stl, or elsewhere that lets me not write this:
class myLess { bool operator() (pair
a, pair b) {return a.first < b.first;} } Thanks for any help, Chris
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Victor A. Wagner Jr. http://rudbek.com The five most dangerous words in the English language: "There oughta be a law"
Deane Yang wrote:
Victor A. Wagner Jr. wrote:
why do you want to sort ONLY on the 1st element of the pair?
I can't speak for the original poster, but I often have a collection of pairs of the form (time, data) and want to put the whole thing in chronological order.
The point is that the ordinary sort already does that.
That's a useful fact I should have known. I apologize and thank you. Peter Dimov wrote:
Deane Yang wrote:
Victor A. Wagner Jr. wrote:
why do you want to sort ONLY on the 1st element of the pair?
I can't speak for the original poster, but I often have a collection of pairs of the form (time, data) and want to put the whole thing in chronological order.
The point is that the ordinary sort already does that.
At Friday 2004-03-26 09:57, you wrote:
I can't speak for the original poster, but I often have a collection of pairs of the form (time, data) and want to put the whole thing in chronological order.
if .first is unique (which it would be in your case) letting the standard operator< for std::pair<> suffices (unless one is extremely hyper about how "fast" things go).
Victor A. Wagner Jr. wrote:
why do you want to sort ONLY on the 1st element of the pair? At Thursday 2004-03-25 11:34, you wrote:
content-class: urn:content-classes:message Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C41297.D800D40A"
I've had this same pattern come up 4 or 5 times now, and each time I've been unable to find a stl + boost solution that allowed me to do what I want without writing my own less function. Basically, I want something that will let me do a sort, or define a map on structs or pairs without writing a less that compares the member variable or ..first. My last attempt:
vector
> myVector std::sort(myVector.begin(), myVector.end(), _1.first < _2.first); No such luck. It looks like I can do operator * and & on lambda objects, but not access members through them. Is there anything in boost, stl, or elsewhere that lets me not write this:
class myLess { bool operator() (pair
a, pair b) {return a.first < b.first;} } Thanks for any help, Chris
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Victor A. Wagner Jr. http://rudbek.com The five most dangerous words in the English language: "There oughta be a law"
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Victor A. Wagner Jr. http://rudbek.com The five most dangerous words in the English language: "There oughta be a law"
participants (6)
-
Chris Jurney
-
Daniel Wallin
-
Deane Yang
-
Duane Murphy
-
Peter Dimov
-
Victor A. Wagner Jr.