Ежедневный бит (е) C++ # 34, Гетерогенная коллекция: std::tuple
std::tuple
представляет собой разнородный набор элементов.
В отличие от std::pair
кортеж может содержать любое количество (в том числе ни одного) элементов.
#include <tuple> #include <vector> void function(int a, int b) {} // Empty std::tuple auto a = std::tuple{}; // std::tuple_size<delctype(a)>{} == 0 auto b = std::tuple{5,42}; // construct a std::vector // using the tuple elements as arguments to the constructor. auto v = std::make_from_tuple<std::vector<int>>(b); // v == {42, 42, 42, 42, 42} // call a function, using the tuple elements as arguments std::apply(function, b); // as with std::pair, std::tuple can // be deconstructed using structured bindings auto c = std::tuple{"Hello World!", 42, &a}; auto [msg, val, ptr] = c; // msg == "Hello World!", val == 42, ptr == &a
Откройте этот пример в Compiler Explorer.