<tuple> 函数

apply

template <class F, class Tuple> constexpr decltype(auto) apply(F&& f, Tuple&& t);

注解

使用元组 t 调用函数 F

forward_as_tuple

template <class... TTypes>
    constexpr tuple<TTypes&&...> forward_as_tuple(TTypes&&...) noexcept;

返回值

返回 tuple<TTypes&&...>(std::forward<TTypes>(t)...)

备注

构造适用于作为参数转发到函数的 t 中参数的引用的元组

get

按索引或类型(在 C++14 中)从 tuple 对象获取元素。

// by index:
// get reference to Index element of tuple
template <size_t Index, class... Types>
   constexpr tuple_element_t<Index, tuple<Types...>>& get(tuple<Types...>& Tuple) noexcept;

// get const reference to Index element of tuple
template <size_t Index, class... Types>
   constexpr const tuple_element_t<Index, tuple<Types...>>& get(const tuple<Types...>& Tuple) noexcept;

// get rvalue reference to Index element of tuple
template <size_t Index, class... Types>
   constexpr tuple_element_t<Index, tuple<Types...>>&& get(tuple<Types...>&& Tuple) noexcept;

// (C++14) by type:
// get reference to T element of tuple
template <class T, class... Types>
   constexpr T& get(tuple<Types...>& Tuple) noexcept;

// get const reference to T element of tuple
template <class T, class... Types>
   constexpr const T& get(const tuple<Types...>& Tuple) noexcept;

// get rvalue reference to T element of tuple
template <class T, class... Types>
   constexpr T&& get(tuple<Types...>&& Tuple) noexcept;

参数

Index
要获取的元素的索引。

类型
元组中声明的序列顺序,按声明顺序排列。

T
要获取的元素的类型。

Tuple
包含任意数目元素的 std::tuple

备注

模板函数返回对索引 Index 处的值或 tuple 对象中类型为 T 的值的引用

如果元组中包含的 T 类型元素的个数不为一个,则调用 get<T>(Tuple) 将生成编译器错误。

示例

#include <tuple>
#include <iostream>
#include <string>

using namespace std;

int main() {
    tuple<int, double, string> tup(0, 1.42, "Call me Tuple");

    // get elements by index
    cout << " " << get<0>(tup);
    cout << " " << get<1>(tup);
    cout << " " << get<2>(tup) << endl;

    // get elements by type
    cout << " " << get<int>(tup);
    cout << " " << get<double>(tup);
    cout << " " << get<string>(tup) << endl;
}
0 1.42 Call me Tuple
0 1.42 Call me Tuple

make_from_tuple

template <class T, class Tuple> constexpr T make_from_tuple(Tuple&& t);

备注

return make_from_tuple_impl<T>(forward<Tuple>(t), make_index_sequence<tuple_size_v<decay_t<Tuple>>>{}) 相同。

make_tuple

从元素值中生成一个 tuple

template <class T1, class T2, ..., class TN>
   tuple<V1, V2, ..., VN> make_tuple(const T1& t1, const T2& t2, ..., const TN& tN);

参数

TN
第 N 个函数参数的类型。

tN
第 N 个函数参数的值。

备注

模板函数返回tuple<V1, V2, ..., VN>(t1, t2, ..., tN),其中每个类型是X&相应的类型Vi cv Ti reference_wrapper<X>时;否则为 。Ti

make_tuple 的优势之一在于要存储的对象类型由编译器自动确定,而不必显式指定。 使用 make_tuple<int, int>(1, 2) 时请不要使用显式模板参数(如 make_tuple),因为它冗长而多余并会增加复杂的右值引用问题,可能会导致编译失败。

示例

// std__tuple__make_tuple.cpp
// compile by using: /EHsc
#include <tuple>
#include <iostream>

typedef std::tuple<int, double, int, double> Mytuple;
int main() {
    Mytuple c0(0, 1, 2, 3);

// display contents " 0 1 2 3"
    std::cout << std::get<0>(c0) << " ";
    std::cout << std::get<1>(c0) << " ";
    std::cout << std::get<2>(c0) << " ";
    std::cout << std::get<3>(c0) << std::endl;

    c0 = std::make_tuple(4, 5, 6, 7);

// display contents " 4 5 6 7"
    std::cout << std::get<0>(c0) << " ";
    std::cout << std::get<1>(c0) << " ";
    std::cout << std::get<2>(c0) << " ";
    std::cout << std::get<3>(c0) << std::endl;

    return (0);
}
0 1 2 3
4 5 6 7

swap

template <class... Types>
    void swap(tuple<Types...>& x, tuple<Types...>& y) noexcept(see below );

tie

从元素引用中生成一个 tuple

template <class T1, class T2, ..., class TN>
tuple<T1&, T2&, ..., TN&> tie(T1& t1, T2& t2, ..., TN& tN);

参数

TN
第 N 个元组元素的基类型。

注解

此模板函数返回 tuple<T1&, T2&, ..., TN&>(t1, t2, ..., tN)

示例

// std__tuple__tie.cpp
// compile with: /EHsc
#include <tuple>
#include <iostream>

typedef std::tuple<int, double, int, double> Mytuple;
int main() {
    Mytuple c0(0, 1, 2, 3);

// display contents " 0 1 2 3"
    std::cout << " " << std::get<0>(c0);
    std::cout << " " << std::get<1>(c0);
    std::cout << " " << std::get<2>(c0);
    std::cout << " " << std::get<3>(c0);
    std::cout << std::endl;

    int v4 = 4;
    double v5 = 5;
    int v6 = 6;
    double v7 = 7;
    std::tie(v4, v5, v6, v7) = c0;

// display contents " 0 1 2 3"
    std::cout << " " << v4;
    std::cout << " " << v5;
    std::cout << " " << v6;
    std::cout << " " << v7;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0 1 2 3

tuple_cat

template <class... Tuples> constexpr tuple<CTypes...> tuple_cat(Tuples&&...);

返回值

通过初始化每个类型元素构造的元组对象。

tuple_element_t

template <size_t I, class T>
    using tuple_element_t = typename tuple_element<I, T>::type;

tuple_size_v

template <class T>
    inline constexpr size_t tuple_size_v = tuple_size<T>::value;