定义于头文件 <tuple>

template< class... Types >
class tuple;

(C++11 起)

类模板 std::tuple 是固定大小的异类值汇集。它是 std::pair 的推广。

若 (std::is_trivially_destructible_v<Types> && ...) 为 true ,则 tuple 的析构函数是平凡的。

(C++17 起)

模板形参

Types...-tuple 所存储的元素的类型。支持空列表。

元组式访问指定的元素

std::get(std::tuple)
template< std::size_t I, class... Types >

typename std::tuple_element<I, tuple<Types...> >::type&

    get( tuple<Types...>& t ) noexcept;
(1)(C++11 起)
(C++14 起为 constexpr)
template< std::size_t I, class... Types >

typename std::tuple_element<I, tuple<Types...> >::type&&

    get( tuple<Types...>&& t ) noexcept;
(2)(C++11 起)
(C++14 起为 constexpr)
template< std::size_t I, class... Types >

typename std::tuple_element<I, tuple<Types...> >::type const&

    get( const tuple<Types...>& t ) noexcept;
(3)(C++11 起)
(C++14 起为 constexpr)
template< std::size_t I, class... Types >

typename std::tuple_element<I, tuple<Types...> >::type const&&

    get( const tuple<Types...>&& t ) noexcept;
(4)(C++11 起)
(C++14 起为 constexpr)

template< class T, class... Types >
constexpr T& get(tuple<Types...>& t) noexcept;

(5)(C++14 起)

template< class T, class... Types >
constexpr T&& get(tuple<Types...>&& t) noexcept;

(6)(C++14 起)

template< class T, class... Types >
constexpr const T& get(const tuple<Types...>& t) noexcept;

(7)(C++14 起)

template< class T, class... Types >
constexpr const T&& get(const tuple<Types...>&& t) noexcept;

(8)(C++14 起)

1-4) 从 tuple 提取第 I 个元素。 I 是 [0, sizeof...(Types)) 中的整数值。

5-8) 提取 tuple t 的类型为 T 的元素。若 tuple 不恰好拥有一个该类型元素则编译失败。

参数

t-要提取内容的 tuple

返回值

到 t 的被选中元素的引用。

调用示例

#include <tuple>
#include <iostream>
#include <string>
#include <typeinfo>
#include <stdexcept>
#include <iostream>
#include <iterator>
#include <vector>
#include <memory>
#include <set>
#include <map>

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}
    Cell(const Cell &cell)
    {
        x = cell.x;
        y = cell.y;
    }

    Cell &operator+(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator+=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator*=(int n)
    {
        x *= n;
        y *= n;
        return *this;
    }

    Cell &operator++()
    {
        x += 1;
        y += 1;
        return *this;
    }

    //std::tie 能用于引入字典序比较到结构体,或解包 tuple :
    bool operator <(const Cell &cell) const
    {
        return std::tie(x, y) < std::tie(cell.x, cell.y);
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

// 打印任何大小 tuple 的辅助函数
template<class Tuple, std::size_t N>
struct TuplePrinter
{
    static void print(const Tuple& t)
    {
        TuplePrinter < Tuple, N - 1 >::print(t);
        std::cout << ", " << std::get < N - 1 > (t);
    }
};

template<class Tuple>
struct TuplePrinter<Tuple, 1>
{
    static void print(const Tuple& t)
    {
        std::cout << std::get<0>(t);
    }
};

template<class... Args>
void print(const std::tuple<Args...>& t)
{
    std::cout << "(";
    TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
    std::cout << ")" << std::endl;
}
// 辅助函数结束

int main()
{
    //1-4) 从 tuple 提取第 I 个元素。 I 是 [0, sizeof...(Types)) 中的整数值。
    std::tuple<int, std::string, float> tuple1(10, "Test", 3.14);
    std::cout << "tuple1 " << typeid(tuple1).name() << ":   " << std::endl;
    print(tuple1);

    // 基于下标的访问
    std::cout << "std::get<0>(tuple1):  " << std::get<0>(tuple1) << std::endl;
    std::cout << "std::get<1>(tuple1):  " << std::get<1>(tuple1) << std::endl;
    std::cout << "std::get<2>(tuple1):  " << std::get<2>(tuple1) << std::endl;

    std::tuple<Cell, std::string, double> tuple2 = std::make_tuple(Cell{101, 101}, "GGX", 3.14);
    std::cout << "tuple2 " << typeid(tuple2).name() << ":   " << std::endl;
    std::cout << "std::get<0>(tuple2):  " << std::get<0>(tuple2) << std::endl;
    std::cout << "std::get<1>(tuple2):  " << std::get<1>(tuple2) << std::endl;
    std::cout << "std::get<2>(tuple2):  " << std::get<2>(tuple2) << std::endl;

    return 0;
}

输出

tuple1 St5tupleIJiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEfEE:
(10, Test, 3.14)
std::get<0>(tuple1):  10
std::get<1>(tuple1):  Test
std::get<2>(tuple1):  3.14
tuple2 St5tupleIJ4CellNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEdEE:
std::get<0>(tuple2):  {101,101}
std::get<1>(tuple2):  GGX
std::get<2>(tuple2):  3.14

用 tie 解包 tuple 时用来跳过元素的占位符

std::ignore

const /*unspecified*/ ignore;

(C++11 起)
(C++17 前)

inline constexpr /*unspecified*/ ignore;

(C++17 起)

 任何值均可赋给而无效果的未指定类型的对象。目的是令 std::tie 在解包 std::tuple 时作为不使用的参数的占位符使用。

调用示例

#include <tuple>
#include <iostream>
#include <string>
#include <typeinfo>
#include <stdexcept>
#include <iostream>
#include <iterator>
#include <vector>
#include <memory>
#include <set>
#include <map>

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}
    Cell(const Cell &cell)
    {
        x = cell.x;
        y = cell.y;
    }

    Cell &operator+(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator+=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator*=(int n)
    {
        x *= n;
        y *= n;
        return *this;
    }

    Cell &operator++()
    {
        x += 1;
        y += 1;
        return *this;
    }

    //std::tie 能用于引入字典序比较到结构体,或解包 tuple :
    bool operator <(const Cell &cell) const
    {
        return std::tie(x, y) < std::tie(cell.x, cell.y);
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

// 打印任何大小 tuple 的辅助函数
template<class Tuple, std::size_t N>
struct TuplePrinter
{
    static void print(const Tuple& t)
    {
        TuplePrinter < Tuple, N - 1 >::print(t);
        std::cout << ", " << std::get < N - 1 > (t);
    }
};

template<class Tuple>
struct TuplePrinter<Tuple, 1>
{
    static void print(const Tuple& t)
    {
        std::cout << std::get<0>(t);
    }
};

template<class... Args>
void print(const std::tuple<Args...>& t)
{
    std::cout << "(";
    TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
    std::cout << ")" << std::endl;
}
// 辅助函数结束

int main()
{
    std::set<Cell> set_of_Cell;
    bool inserted = false;
    //任何值均可赋给而无效果的未指定类型的对象。
    //目的是令 std::tie 在解包 std::tuple 时作为不使用的参数的占位符使用。
    std::tie(std::ignore, inserted) = set_of_Cell.insert(Cell{1024, 1024});
    if (inserted)
    {
        std::cout << "Value was inserted successfully" << std::endl;
    }

    return 0;
}

输出

Value was inserted successfully

更多推荐