Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does there exist something like std::tie for std::pair?

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; 
like image 403
wrhall Avatar asked Jul 07 '15 15:07

wrhall


People also ask

What is std :: pair?

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.

Is std :: pair a container?

std::pair is not a Container.

How is std :: tie implemented?

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 .


1 Answers

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; 
like image 159
lisyarus Avatar answered Sep 22 '22 07:09

lisyarus