TR1 tuple

Hi Guys,

I started reading on TR1::tuple. Actually, the impression I got after reading one tutorial of it was that one of it's main uses is for returning two values from the function. Now, can we not achieve this same thing using struct or a class for that matter? Can someone please explain how tuple is different in this context? and when exactly we use it?

Thanks so much
It just saves you some typing, and make things more generic.

1
2
3
tuple<int, double, int, string> f(tuple<string, double, vector<int>, string> t) {
    //...
}


instead of

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct type_1 {
    int i;
    double d;
    int i2;
    string str;
    type_1(int i, double d, int i2, string str) : i(i), d(d), i2(i2), str(str) {}
}
// Same with the other tupe type.
struct type_2 {
    //...
}

type_1 f(type_2 t) {
    //...
}


Now imagine, you have to change some type in one of your tuples. How many places do you have to re-write in the first, and in the second version?
Thanks for the reply R0mai. So that means there is no difference between using struct & tuple, other than the fact that tuple will save some typing ?
typedef tuple<int, double, int, string> type_1; :)
Classes/structs are in the current standard. tuples are not (yet). pairs are, in case you wanted to return exactly two values.

Note that multiple returns can also be achieved with passing by reference.
Last edited on
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).
Thanks so much for the reply moorecm and jsmith.

So, is it good idea to have something like:

1
2
3
tuple<string, vector<string> >foo(string str, char delim) { 

} 


Edit: Also what is empty tuple used for?
Last edited on
Here's a recent thread on this subject:
http://www.cplusplus.com/forum/beginner/18489/

The only good reason to use a tuple is when you don't know what will be returned. That's right. Hence, if your function returns an empty tuple, that means that there were no results.

(Well, there actually are other reasons to use tuples...)
Thanks so much guys !
Topic archived. No new replies allowed.