introduce
Tuple tupleIt is a template in C++. The member types of different tuple types are also different, but a tuple can have any number of members.
The number of members of each tuple type isA certain, but the number of different tuple members can be different.
Definition and initialization of tuple
Use tuple to import tuple header files
#include <tuple>
Tuple definition and initialization (we take the member types as int, string, and vector as examples)
tuple<int,string,vector<int>> test{1,"hello,world",{4,5,6}};
Of course, it can also be initialized using its constructor
tuple<int, string,vector<int>> test(1,"hello,world",{4,5,6});
There is a situation where it appears that no initialization is performed on the surface, but it actually utilizes the default constructor without parameters.
tuple<size_t, size_t> test; //The value of each member is0
Of course, if you want to change the initial value of test after you define it like this, you can write it like this, as shown below. (In fact, the current test is not the original test, it is replaced by your newly created tuple object. Strictly speaking, it is not considered initialization)
tuple<size_t, size_t> test; //The value of each member is 0test = tuple<size_t, size_t>(1,2); //The value of the first member is1,The value of the second member is2
In addition, the standard library defines the make_tuple function to generate tuple objects. We can use the auto keyword to infer the type of generated tuple
auto test2 = make_tuple(3.14,"wasd");
If you want to add a vector as a member, the situation is as follows
//Error writingauto test2 = make_tuple(3.14,"wasd",{ 3,4,5 }); //Correct writingvector<int> nums = { 3,4,5 }; auto test2 = make_tuple(3.14,"wasd",nums);
Use of tuple
Member visits
When it comes to the use of tuple, the most direct thought is naturally the access to tuple.
In the C++ standard library, there is a function template called get. In order to use get, we must specify aExplicit template argumentsto indicate the accessed member and give it a tuple object in the function parameter.
tuple<int, string, vector<int>> test{ 1,"hello,world",{4,5,6} }; cout << get<0>(test) << endl; //Print the first member of test, its type is intcout << get<1>(test) << endl; //Print the second member of the test, its type is stringcout << get<2>(test)[0] << endl; //PrinttestThird membervector<int>The first element of
Here are the results returned
1
hello,world
4
Get tuple information
You can obtain the number of tuple elements as follows
tuple<int, double, vector<string>> test{ 1,1.23,{"123","456"}}; //Get the number of tuple memberssize_t num = tuple_size<decltype(test)>::value; //Use a certain member type of tuple to declare variables (taking the 0th member as an example)tuple_element<0, decltype(test)>::type n = get<0>(test);
Splicing tuple
tuple<int, int, int> test(1,2,3); tuple<int, int, int> test2(4,5,6); tuple<int, int, int> test3(7,8,9); //Splitauto new_tup = tuple_cat(test, test2, test3);
Exchange tuple
tuple<int, int, int> test(1,2,3); tuple<int, int, int> test2(4,5,6); //exchange(test2);
Tuple Unpacking
Tuple uses the tie method to unpack,The number of parameters of the tie is the same as the number of tuple members, otherwise you must use ignore to occupy the position
tuple<int,int> test{2,8}; tuple<int,int> test2{2,8,6}; int a, b; //Unpackagetie(a, b) = test; //ignore placeholding situationtie(a, b, ignore) = test2;
Tuple comparison
Tuple comparison is legal when two tuples have the same number of members and the corresponding member types are comparable.
//There are four tuples as followstuple<string, string> test{"1","2"}; tuple<int, int, int> test2{ 1,2,3 }; tuple<int,int> test3{1,2}; tuple<int, int> test4{ 4,5 }; test == test2; //An error is reported, string and int types cannot be comparedtest2 < test3; // Report an error, test2 and test3 members are differenttest3 < test4; //correct,The value of this statement isTrue
Tuple traversal
Tuple does not have an iterator, and its traversal is very troublesome, and its design purpose is not to be this (if you want to traverse a certain data structure during project development, you should try to avoid being tuple type as much as possible, and you can use list instead)
But here is still a traversal method (not recommended)
This method comes fromDetailed usage of C++ Tuple tuple - Zhihu ()
#include <iostream> #include <tuple> #include <array> #include <utility> using namespace std; template<class Tuple, size_t N> struct PrintTuple { static void Printf(const Tuple& Value) { PrintTuple<Tuple, N - 1>::Printf(Value); cout << "," << get<N - 1>(Value); } }; template<class Tuple> struct PrintTuple<Tuple, 1> { static void Printf(const Tuple& Value) { cout << get<0>(Value); } }; template<class... Args> void PrintfMyTuple(const tuple<Args...>& vlaue) { PrintTuple<decltype(vlaue), sizeof...(Args)>::Printf(vlaue); } int main() { tuple<int, int, int, int> a(2, 3, 1, 4); PrintfMyTuple(a); system("pause"); return 0; }
Applications during development of tuple
During project development, if we want a function to return multiple different types of values, we can use tuple.
This is all about this article about C++ tuple tuple type. For more related content about C++ tuple tuple type, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!