A tuple
is a C++11 construction and it is built heavily on variadic templates.
A tuple
is a variadic class template that stores an unlimited set of values of different types, defined when instantiating the tuple; for example:
tuple<int, int> x;</int,>
will store 2 integers.
tuple<string, int, bool> y;</string,>
will store one string, one integer and one boolean and so on.
Tuples are useful for several things:
-
They are light replacements for structures.
-
They can be useful to return several values from a function
- They let you perform comparisons through all the value set
You can instantiate tuples in these ways:
tuple<int, int, bool> x; //instantiating but not initializing<br>tuple<int, string> y { 2, "hello" }; //instantiating AND initializing<br>auto z = make_tuple(2, 3, 4, "bye"s); //instantiating AND initializing throught the make_tuple helper function.</int,></int,>
Consider you declare something like this:
auto xx = make_tuple(3.14, "PI"s);
To get the tuple values, you must to use the get<int>()</int>
function like in this code excerpt:
The template parameter is the index of the element to be retrieved, so, if the first element was an integer, the index 0 will retrieve the integer stored as the first value in the tuple, the index 0 will retrieve the second one and so on.
Tuples are also very useful to create structs “on the fly” and to use them in data structures; for example:
In this case I did not need to create a new struct in order to store the number and the number name, for example.
Other nice thing on tuples is that you can compare tuples relying on the comparison operators of the data types inside the tuples.
Look at this code:
When it is executed, this thing is displayed:
BMW, X5; 2014<br>Chevrolet, Sonic; 2013<br>Toyota, Rav4; 2012<br>VW, Jetta; 2014<br>VW, Jetta; 2015<br>******<br>Toyota, Rav4; 2012
The code and its execution contain several interesting things:
-
I defined an alias to a tuple called “car”, having it, I was able to use the tuple as a declared type in a lot of cases… without declaring it!
-
If I would have created a “car” struct, to use it inside a set, I would have to overload the operator
Now I want to modify my set in order to store the elements ordered by year, by brand and by model. Look at the new implementation:
The output is:
Toyota, Rav4; 2012<br>Chevrolet, Sonic; 2013<br>BMW, X5; 2014<br>VW, Jetta; 2014<br>VW, Jetta; 2015<br>******<br>Toyota, Rav4; 2012
The elements are ordered first by year.
Notice my “cc” lambda expression. It uses a function called “tie”.
“tie” is a function that takes a set of references and creates a tuple containing references (not values or copies) to the original values. So, tie is useful to create temporary light tuples. In my example, using “tie”, I was able to create other set of tuples with different logical order, so the tuple operator
Original: https://www.cnblogs.com/bigben0123/p/15509620.html
Author: Bigben
Title: tuple c++
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/536219/
转载文章受原作者版权保护。转载请注明原作者出处!