c++11 标准模板(STL)(std::tuple)(九)

定义于头文件 <tuple>

template< class... Types >
class tuple;

(C++11 起)

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

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

(C++17 起)

模板形参

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

在编译时获得 tuple 的大小

std::tuple_size<std::tuple>

template< class T >
class tuple_size; /*undefined*/

(1)(C++11 起)
template< class... Types >

class tuple_size< std::tuple<Types...> >

 : public std::integral_constant<std::size_t, sizeof...(Types)> { };
(2)(C++11 起)

定义于头文件 <tuple>

定义于头文件 <array>

(C++17 起)

定义于头文件 <utility>

(C++17 起)

定义于头文件 <ranges>

(C++20 起)

定义于头文件 <span>

(C++20 起)

template< class T >

class tuple_size<const T>

 : public std::integral_constant<std::size_t, tuple_size<T>::value> { };
(3)(C++11 起)
template< class T >

class tuple_size< volatile T >

 : public std::integral_constant<std::size_t, tuple_size<T>::value> { };
(4)(C++11 起)
template< class T >

class tuple_size< const volatile T >

 : public std::integral_constant<std::size_t, tuple_size<T>::value> { };
(5)(C++11 起)

 提供对 tuple 中元素数量的访问,作为编译时常量表达式。

在通过包含 <tuple> 头文件变得合法以外,模板 (3-5) 在包含头文件 <array> 或 <utility> 时可用。

所有 std::tuple_size 的特化满足 BaseCharacteristic 对于某些 Nstd::integral_constant<std::size_t, N>一元类型特征 (UnaryTypeTrait) 。

cv-T 模板 (3-5) 是 SFINAE 友好的:若 std::tuple_size<T>::value 在视作不求值运算数时为病式,则 (3-5) 不提供成员 value 。访问检查如同在无关于 tuple_sizeT 的语境进行。仅考虑表达式直接语境的合法性。这允许:

#include <utility>
struct X { int a, b; };
const auto [x, y] = X(); // 结构化绑定声明首先尝试 tuple_size<const X>
                         // 这试图使用 tuple_size<X>::value ,而那是 OK 的
(C++17 起)

辅助变量模板

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

(C++17 起)

 

继承自 std::integral_constant

成员常量

value

[静态]

sizeof...(Types)
(公开静态成员常量)

成员函数

operator std::size_t

转换对象为 std::size_t ,返回 value
(公开成员函数)

operator()

(C++14)

返回 value
(公开成员函数)

成员类型

类型定义
value_typestd::size_t
typestd::integral_constant<std::size_t, value>

调用示例

#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;
    }

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

    bool operator !=(const Cell &cell) const
    {
        return std::tie(x, y) != std::tie(cell.x, cell.y);
    }

    bool operator <(const Cell &cell) const
    {
        return std::tie(x, y) < std::tie(cell.x, cell.y);
    }

    bool operator <=(const Cell &cell) const
    {
        return std::tie(x, y) <= std::tie(cell.x, cell.y);
    }

    bool operator >(const Cell &cell) const
    {
        return std::tie(x, y) > std::tie(cell.x, cell.y);
    }

    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::tuple<Cell, std::string, float> tuple1(Cell{101, 101}, "Test", 3.14);
    std::cout << "std::tuple_size<decltype(tuple1)>::value: "
              << std::tuple_size<decltype(tuple1)>::value << std::endl;
    std::cout << "tuple1 " << typeid(tuple1).name() << ":   " << std::endl;
    print(tuple1);

    std::tuple<Cell, std::string, float> tuple2 = std::make_tuple(Cell{102, 102}, "GGX", 3.14159);
    std::cout << "std::tuple_size<decltype(tuple2)>::value: "
              << std::tuple_size<decltype(tuple2)>::value << std::endl;
    std::cout << "tuple2 " << typeid(tuple2).name() << ":   " << std::endl;
    print(tuple2);
    std::cout << std::endl;

    return 0;
}

输出

std::tuple_size<decltype(tuple1)>::value: 3
tuple1 St5tupleIJ4CellNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEfEE:
({101,101}, Test, 3.14)
std::tuple_size<decltype(tuple2)>::value: 3
tuple2 St5tupleIJ4CellNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEfEE:
({102,102}, GGX, 3.14159)

  • 36
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值