In C++11 tuple type is collection of heterogeneous (different types) elements of pre-arranged dimensions. tuple is similar to fixed sized vector but the difference is that it can store elements of different types (while vector can store different of only one type).
Let’s see some simple definition of tuple type:
We defined tuple class having fix size. Size of this tuple is sizeof(string) + sizeof(double) + sizeof(int). Variable of our test_tuple type will be able to store three variables of different types (string, double, int) and can be initialized like here:
In order to get value of concrete position of tuple we can use std::get template in the following way:
This instruction will get value of 2nd parametr of myTupleObject variable of tuple type (remember - numering from 0 as usual in C++) and assign that value to test_int variable. In order to set (overwrite) value of concrete position of tuple object need ot use std::get template too: You can to do it this way:
This operation will overwrite second object of myTupleObject (double value 2.5) and will assign there value 3.3. As you probably noticed, tuple types are very similar to struct types. That is true. But in my opinion tuple types are easier to define and maintain. What do you think? Write your opinion and examples of usage tuple types in comments, please.
We defined tuple class having fix size. Size of this tuple is sizeof(string) + sizeof(double) + sizeof(int). Variable of our test_tuple type will be able to store three variables of different types (string, double, int) and can be initialized like here:
In order to get value of concrete position of tuple we can use std::get template in the following way:
This instruction will get value of 2nd parametr of myTupleObject variable of tuple type (remember - numering from 0 as usual in C++) and assign that value to test_int variable. In order to set (overwrite) value of concrete position of tuple object need ot use std::get template too: You can to do it this way:
This operation will overwrite second object of myTupleObject (double value 2.5) and will assign there value 3.3. As you probably noticed, tuple types are very similar to struct types. That is true. But in my opinion tuple types are easier to define and maintain. What do you think? Write your opinion and examples of usage tuple types in comments, please.
Comments
Post a Comment