Summary of the basic usage of C++ tuple tuples
- 2020-10-23 20:11:26
- OfStack
1. Introduction to tuples
tuple is a collection of fixed size values of different types and is generalized std::pair. We can also use it as a generic struct, without having to create a struct to capture the characteristics of the struct, and in some cases to replace the struct to make the program simpler and more intuitive. While std::tuple can theoretically have an infinite number of member variables of any type, std::pair can only have two members, so you need to use tuple tuples when you need to save three or more data.
tuple (tuple) started as a reference in c++11. tuple seems simple, but in fact it is simple but not simple, so to speak, it is one of the simple and complex things in c++11. The simple one is that it is easy to use, while the complex one is that it hides too many details inside, and it is difficult to uncover its mysterious veil.
2. Creation and initialization of tuple
std::tuple<T1, T2, TN> t1; // create 1 empty tuple Object (using the default construct), whose corresponding elements are T1 and T2...Tn Type, initialized with a value.
std::tuple<T1, T2, TN> t2(v1, v2, ... TN); // create 1 a tuple Object whose two elements are T1 and T2 ...Tn type ; To get the value of the element you need to pass through tuple A member of the get<Ith>(obj) To obtain (Ith It means to get in tuple See below for specific examples ) .
std::tuple<T1&> t3(ref&); // tuple Element types of can be 1 A reference
std::make_tuple(v1, v2); // like pair1 It can also pass make_tuple To create a 1 a tuple object
The element type of tuple is a reference:
std::string name;
std::tuple<string &, int> tpRef(name, 30);
// right tpRef The first 1 I'm going to assign a value to, and I'm going to assign a value to name Has been assigned - reference
std::get<0>(tpRef) = "Sven";
// name The output is Sven
std::cout << "name: " << name << '\n';
3. Operations on the tuple element
Equivalent structure
In the beginning, we mentioned that at some point tuple can be used like structure 1, which is convenient and fast. Such as:
struct person {
char *m_name;
char *m_addr;
int *m_ages;
};
// You can use tuple To represent something like this 1 Three structure types, the function is 1 Kind of.
std::tuple<const char *, const char *, int>
How do I get the number of tuple elements
When there is 1 tuple object but it is not known how many elements can be queried as follows:
// tuple_size
#include <iostream> // std::cout
#include <tuple> // std::tuple, std::tuple_size
int main ()
{
std::tuple<int, char, double> mytuple (10, 'a', 3.14);
std::cout << "mytuple has ";
std::cout << std::tuple_size<decltype(mytuple)>::value;
std::cout << " elements." << '\n';
return 0;
}
// Output results:
mytuple has 3 elements
How do I get the value of an element
Get the value of the tuple object element via get < Ith > (obj) method;
Ith - is the location of the element you want to get in the tuple object.
obj - is the object that you want to get tuple
// tuple_size
#include <iostream> // std::cout
#include <tuple> // std::tuple, std::tuple_size
int main ()
{
std::tuple<int, char, double> mytuple (10, 'a', 3.14);
std::cout << "mytuple has ";
std::cout << std::tuple_size<decltype(mytuple)>::value;
std::cout << " elements." << '\n';
// Access to elements
std::cout << "the elements is: ";
std::cout << std::get<0>(mytuple) << " ";
std::cout << std::get<1>(mytuple) << " ";
std::cout << std::get<2>(mytuple) << " ";
std::cout << '\n';
return 0;
}
// Output results:
mytuple has 3 elements.
the elements is: 10 a 3.14
tuple does not support iteration and can only get the value of an element by element index (or tie unpacking). But the given index must have been given at the compiler and cannot be passed dynamically at run time, otherwise a compilation error will occur:
for(int i=0; i<3; i++)
std::cout << std::get<i>(mytuple) << " "; // A compilation error will be raised
Gets the type of the element
The element type can be obtained by using the tuple_element method, as shown in the following tuple objects:
std::tuple<std::string, int> tp("Sven", 20);
// Get the first 2 Individual element types
std::tuple_element<1, decltype(tp)>::type ages; // ages for int type
ages = std::get<1>(tp);
std::cout << "ages: " << ages << '\n';
// Output results:
ages: 20
Use tie to unpack the element values
As with pair1, the values of the elements of tuple can also be unpacked using tie. The following tuple object has four elements, and unpacking through tie will assign the values of these four elements to the four variables provided by tie.
#include <iostream>
#include <tuple>
#include <utility>
int main(int argc, char **argv) {
std::tuple<std::string, int, std::string, int> tp;
tp = std::make_tuple("Sven", 25, "Shanghai", 21);
// Define the receiving variable
std::string name;
std::string addr;
int ages;
int areaCode;
std::tie(name, ages, addr, areaCode) = tp;
std::cout << "Output: " << '\n';
std::cout << "name: " << name <<", ";
std::cout << "addr: " << addr << ", ";
std::cout << "ages: " << ages << ", ";
std::cout << "areaCode: " << areaCode << '\n';
return 0;
}
// Output results:
Output:
name: Sven, addr: Shanghai, ages: 25, areaCode: 21
However, sometimes only one or two elements are needed when tuple contains multiple elements, so variable placeholders can be performed via std::ignore, which will ignore the extraction of corresponding elements. You can modify the above routine:
#include <iostream>
#include <tuple>
#include <utility>
int main(int argc, char **argv) {
std::tuple<std::string, int, std::string, int> tp;
tp = std::make_tuple("Sven", 25, "Shanghai", 21);
// Define the receiving variable
std::string name;
std::string addr;
int ages;
int areaCode = 110;
std::tie(name, ages, std::ignore, std::ignore) = tp;
std::cout << "Output: " << '\n';
std::cout << "name: " << name <<", ";
std::cout << "addr: " << addr << ", ";
std::cout << "ages: " << ages << ", ";
std::cout << "areaCode: " << areaCode << '\n';
return 0;
}
// Output results:
Output:
name: Sven, addr: , ages: 25, areaCode: 110
Reference to the tuple element
The element type that references tuple has been enumerated previously. You can extract the element value of tuple by referring to it with make_tuple(), set certain variable values to them, and change the value of the tuple element by changing these variables:
#include <iostream>
#include <tuple>
#include <functional>
int main(int argc, char **agrv) {
std::tuple<std::string, int, float> tp1("Sven Cheng", 77, 66.1);
std::string name;
int weight;
float f;
auto tp2 = std::make_tuple(std::ref(name), std::ref(weight), std::ref(f)) = tp1;
std::cout << "Before change: " << '\n';
std::cout << "name: " << name << ", ";
std::cout << "weight: " << weight << ", ";
std::cout << "f: " << f << '\n';
name = "Sven";
weight = 80;
f = 3.14;
std::cout << "After change: " << '\n';
std::cout << "element 1st: " << std::get<0>(tp2) << ", ";
std::cout << "element 2nd: " << std::get<1>(tp2) << ", ";
std::cout << "element 3rd: " << std::get<2>(tp2) << '\n';
return 0;
}
// Output results:
Before change:
name: Sven Cheng, weight: 77, f: 66.1
After change:
element 1st: Sven, element 2nd: 80, element 3rd: 3.14
tuple exchange
std::string name;
std::tuple<string &, int> tpRef(name, 30);
// right tpRef The first 1 I'm going to assign a value to, and I'm going to assign a value to name Has been assigned - reference
std::get<0>(tpRef) = "Sven";
// name The output is Sven
std::cout << "name: " << name << '\n';
0
The sorting
std::string name;
std::tuple<string &, int> tpRef(name, 30);
// right tpRef The first 1 I'm going to assign a value to, and I'm going to assign a value to name Has been assigned - reference
std::get<0>(tpRef) = "Sven";
// name The output is Sven
std::cout << "name: " << name << '\n';
1