tr1::tuple is simply a generalization of std::pair.
The advantage of tuple is as moorecm says: it saves you from having to write a full-fledged value class/struct:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
struct my_special_tuple {
my_special_tuple() : elem1(), elem2(), elem3() {}
my_special_tuple( int elem1, double elem2, const std::string elem3 ) :
elem1( elem1 ), elem2( elem2 ), elem3( elem3 ) {}
friend std::ostream&( std::ostream& os, const my_special_tuple& t ) {
return os << '[' << t.elem1 << ',' << t.elem2 << ',' << t.elem3 << ']';
}
int elem1;
double elem2;
std::string elem3;
};
|
tuple gives all of the above to you already, and all you need to type is
|
typedef tr1::tuple<int, double, std::string > my_special_tuple;
|
The drawback to tuple is that you can't control the "names" of the fields.
I used elem1, elem2, and elem3, but typically you'd use something more
descriptive, like "age", "height", and "name".
With tuple, you would use
|
my_tuple.get<0>(); // to get the first element
|
(though IIRC you might be able to create more meaningful tags instead of
using numbers, like boost::multi_index_container allows).