Eg with tuples:
#include <tuple> // std::tuple, std::make_tuple, std::tie int num; char letter; std::tuple<int,char> num_letter; num_letter = std::make_tuple(10, 'a'); std::tie(num, letter) = num_letter; // unpack num_letter into num and letter
Is there something equivalent with pairs?
// ... num_letter = std::make_pair(10, 'a'); std::pair_tie(num, letter) = num_letter;
std::pair is a class template that provides a way to store two heterogeneous objects as a single unit. A pair is a specific case of a std::tuple with two elements.
std::pair is not a Container.
So basically, std::tie(a) initializes a data member reference to a . std::tuple<int>(24) creates a data member with value 24 , and the assignment assigns 24 to the data member reference in the first structure. But since that data member is a reference bound to a , that basically assigns 24 to a .
Actually, the code for pairs is exactly the same, since std::tuple
has operator =
with std::pair
as an argument.
num_letter = std::make_pair(10, 'a'); std::tie(num, letter) = num_letter;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With