How to sort a vector of tuples

Greetings,
I have no idea why the following code does not work. Seems to work fine
when I try sorting a vector of ints instead. I have even overloaded the
< operator. If I understand correctly, this is the only requirement to
sort a userdefined class. The compiler gets mad and spits out a a whole
bunch of errors.
thanx
#include "boost/tuple/tuple_io.hpp"
#include <vector>
#include <algorithm>
typedef boost::tuple

Sumanth, Comparison operators are defined in tuple_comparison.hpp Sumanth J.V wrote:
#include "boost/tuple/tuple_io.hpp"
// add this line #include "boost/tuple/tuple_comparison.hpp"
#include <vector> #include <algorithm>
typedef boost::tuple
Task; typedef std::vector<Task> TaskSet;
// remove these // bool operator<(const Task& a, const Task& b) { // return boost::get<1>(a) < boost::get<1>(b); // }
int main(int argc, char** argv) { TaskSet ts;
ts.push_back( boost::make_tuple(0, 0, 0, 0) ); ts.push_back( boost::make_tuple(3, 3, 3, 3) ); ts.push_back( boost::make_tuple(2, 2, 2, 2) );
std::sort(ts.begin(), ts.end());
return 0; }
KevinH -- Kevin Heifner heifner @ ociweb.com http://heifner.blogspot.com Object Computing, Inc. (OCI) www.ociweb.com

Kevin Heifner wrote:
Sumanth,
Comparison operators are defined in tuple_comparison.hpp
Sumanth J.V wrote:
#include "boost/tuple/tuple_io.hpp"
// add this line #include "boost/tuple/tuple_comparison.hpp"
#include <vector> #include <algorithm>
typedef boost::tuple
Task; typedef std::vector<Task> TaskSet; // remove these // bool operator<(const Task& a, const Task& b) { // return boost::get<1>(a) < boost::get<1>(b); // }
int main(int argc, char** argv) { TaskSet ts;
ts.push_back( boost::make_tuple(0, 0, 0, 0) ); ts.push_back( boost::make_tuple(3, 3, 3, 3) ); ts.push_back( boost::make_tuple(2, 2, 2, 2) );
std::sort(ts.begin(), ts.end());
return 0; }
KevinH
Thanx Kevin, Now my code compiles. However, I would like to change the default behavior of the comparison operator. From the boost documentation, "the operators <, >, <= and >= implement a lexicographical ordering". But I would like comparison to be based only on the second element of the tuple. Is there anyway this can be implemented? cheers sumanth

Hi! Sumanth J.V wrote:
Now my code compiles. However, I would like to change the default behavior of the comparison operator. From the boost documentation, "the operators <, >, <= and >= implement a lexicographical ordering". But I would like comparison to be based only on the second element of the tuple. Is there anyway this can be implemented?
To this end, you may supply a custom comparison operator to std::sort. struct my_compare_op { bool operator()(const Task& a, const Task& b) const { return boost::get<1>(a) < boost::get<1>(b); } }; and then, std::sort(ts.begin(), ts.end(), my_compare_op()); HTH, João Abecasis

"Sumanth J.V"
Thanx Kevin,
Now my code compiles. However, I would like to change the default behavior of the comparison operator.
You can't. That's like wanting to change the comparison behavior for int. The library provided the tuple type, and it provided comparison semantics, so that's what you get. If you want something else, you can make the tuple a member inside a class of your own choice, or you can supply an explicit comparison function when sorting. -- Dave Abrahams Boost Consulting www.boost-consulting.com

Thanx a lot everyone for your responses. It really helped me understand STL/Boost better. I am a primarily a C programmer and was not familiar with the idea of a functor. But after reading the SGI STL docs in more detail it all makes sense now :). cheers sumanth
participants (4)
-
David Abrahams
-
Joao Abecasis
-
Kevin Heifner
-
Sumanth J.V