API Reference: `std::integer_sequence

C++ 工具库提供的 std::integer_sequence 类模板用于表示一个编译时的整数序列,常用于模板元编程中的参数包展开。此类型定义于 <utility> 头文件。

定义

template< class T, T... Ints >
struct integer_sequence;
  • C++14 起
  • 类模板 std::integer_sequence 用于存储编译时确定的整数序列。

模板形参

  • T: 序列中元素的整数类型。
  • ...Ints: 表示序列的非类型形参包。

成员类型

  • value_type: 类型 T,用于表示序列中的元素类型。

成员函数

size

static constexpr std::size_t size() noexcept;
  • 返回序列 Ints 中的元素数量。
  • 实现等价于 sizeof...(Ints)

辅助模板

std::index_sequence

std::size_t 类型的序列定义的别名模板:

template<std::size_t... Ints>
using index_sequence = std::integer_sequence<std::size_t, Ints...>;

std::make_integer_sequencestd::make_index_sequence

用于生成从 0 到 N-1 的序列:

template<class T, T N>
using make_integer_sequence = std::integer_sequence<T, /* a sequence 0, 1, 2, ..., N-1 */>;

template<std::size_t N>
using make_index_sequence = std::make_integer_sequence<std::size_t, N>;
  • N 为负值则构造非法。
  • N 为零,则结果类型为 integer_sequence<T>

std::index_sequence_for

根据类型参数包的大小生成相应长度的下标序列:

template<class... T>
using index_sequence_for = std::make_index_sequence<sizeof...(T)>;

示例

#include <tuple>
#include <iostream>
#include <array>
#include <utility>

// 打印序列
template<typename T, T... ints>
void print_sequence(std::integer_sequence<T, ints...> int_seq) {
    std::cout << "The sequence of size " << int_seq.size() << ": ";
    ((std::cout << ints << ' '), ...);
    std::cout << '\n';
}

// 数组转换为 tuple
template<typename Array, std::size_t... I>
auto a2t_impl(const Array& a, std::index_sequence<I...>) {
    return std::make_tuple(a[I]...);
}

template<typename T, std::size_t N, typename Indices = std::make_index_sequence<N>>
auto a2t(const std::array<T, N>& a) {
    return a2t_impl(a, Indices{});
}

// 打印 tuple
template<class Ch, class Tr, class Tuple, std::size_t... Is>
void print_tuple_impl(std::basic_ostream<Ch,Tr>& os, const Tuple& t, std::index_sequence<Is...>) {
    ((os << (Is == 0? "" : ", ") << std::get<Is>(t)), ...);
}

template<class Ch, class Tr, class... Args>
auto& operator<<(std::basic_ostream<Ch, Tr>& os, const std::tuple<Args...>& t) {
    os << "(";
    print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
    return os << ")";
}

int main() {
    print_sequence(std::integer_sequence<unsigned, 9, 2, 5, 1, 9, 1, 6>{});
    print_sequence(std::make_integer_sequence<int, 20>{});
    print_sequence(std::make_index_sequence<10>{});
    print_sequence(std::index_sequence_for<float, std::iostream, char>{});

    std::array<int, 4> array = {1, 2, 3, 4};
    auto tuple = a2t(array); // 数组转 tuple
    static_assert(std::is_same<decltype(tuple), std::tuple<int, int, int, int>>::value, "");
    std::cout << tuple << '\n';
}

输出示例:

The sequence of size 7: 9 2 5 1 9 1 6 
The sequence of size 20: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
The sequence of size 10: 0 1 2 3 4 5 6 7 8 9 
The sequence of size 3: 0 1 2 
(1, 2, 3, 4)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值