Hello again!
In this video, we are going to look at tuples.
Tuples in C++ are related to pairs.
So let's first remind ourselves what those do.
A pair is a compound type.
It has two members, which we can access with the names first and second.
These members can have any type.
We can create a pair object explicitly, by giving the types of the elements as template parameters.
So here we have a pair which has two members, of type string.
Or we can use make_pair() to create the pair object, with auto.
So in this case, the compiler will deduce that word_pair is a pair, whose members are both type string.
In C++11, we have a tuple type in the library.
It is defined in the header.
It is similar to the pair, but it can have any number of elements.
The number of elements has to be fixed for a particular object.
It is not really practical to use the names of members, so instead we use the index for accessing the
elements.
Again, we can create a tuple object explicitly, by giving the types of the members as template parameters.
Or we can use make_tuple() and let the compiler deduce it for us.
The way that we access tuple members is a bit peculiar.
There is a non-member function, called get(). This takes the tuple as argumen. And then it has a template
parameter, which is the index of the element.
So if we call get() with parameter zero, that will be the first element. And then we pass the
"numbers" tuple.
So this will return the first element of the numbers tuple.
And we can also assign to the return value from get().
So we can actually use get() to set the value of an element. Which is perhaps rather confusing naming.
But that is quite common in C++, I'm afraid.
So in this case, we are getting the second element of numbers, and setting its value to three.
In C++14,
we can also use the type of the parameter, provided there is only one elements which has that type.
So we can call get() with parameter int, and that will return the member of numbers which has type int.
So let's try that out.
We create our tuple.
We pass the types of the elements as template parameters. Or we could also calle make_tuple().
There is our call to get(), with parameter zero.
So this is going to return the first element of this tuple.
get() with parameter
one will be the second member of this tuple. And then we can set that to three.
If we have the parameter type of int, then this will return the member which has type int, which is this
one.
We just set that to three.
So we would expect that this call will return three.
And there we are.
First elements is one.
Second element, set to three.
It is also possible to get all the elements of a tuple with a single call.
There is a non-member function called tie() which does that.
And again, this is a bit peculiar.
We need to have some variables, which we are going to use to store the data.
Then we pass those variables as arguments to tie(), and then we assign the result of calling tie().
So this is going to extract all the elements from number, and then store the first element in "d", the
second element in "i" and the third elements in "str".
And obviously, the number and type of these variables must match the elements of the tuple.
So let's try that out.
We have the same tuple again.
We have the variables, which we are going to populate.
Then we call tie().
We pass the variables. And then we assign this to the numbers tuple, and then we print out the result.
So there we are.
The tuple elements are one, two and three.
So when would you want to use a tuple?
One good example is, if you have some data, and you just want to stash it somewhere, without too much trouble.
If you do not wants to write a class and think about member functions, special member functions and
so on. And you are not actually going to use that type anywhere else.
And this is especially useful if the data has elements of different types.
Otherwise you could use a vector, or an array, or some other container.
Another good example is returning multiple values from a function call.
This is a question that I have been asked at interviews.
If you have exactly two values, you could use a pair. But in the general case, you have to create
a struct.
Think up a unique name for it in that namespace.
Then think of names for all the members, then populate all the members in the function body, and then
in the caller.
You have to copy all the data from the members of the struct into your own variables. Which is all
a bit tedious.
With the tuple, you can do it much more conveniently.
You can have a function which returns a tuple, so you just make the parameters the right type for your
data.
You can use a list initializer for creating this tuple object.
So you could do that with variables, as well as literals.
And then, in the caller, you just call tie(). And then this will copy the data, from the returned tuple,
into your variables.
Unfortunately, we cannot use auto, at least not in C++14, because tie() is not able to deduce the types
of the elements. It is just too difficult.
So here is that code.
Here is our function which returns a tuple.
Then we create this tuple using the list
initializer.
Then we have the code which unpacks the tuple into these variables. And we unpack the tuple which was
returned by the function.
So this should give us the data from the function. And there we are.
Okay, so that is it for this video.
I will see you next time.
But until then, keep coding!