基于 std::array 的多维数组
multi-dimensional array based on std::array
我需要一个基于std::array
给出多维数组的模板。
template <typename T, size_t...>
using MyArray = ? // -> here is something I don't know how to write...
用法如下:
static_assert(std::is_same_v<
MyArray<int, 2>, std::array<int, 2>
>);
static_assert(std::is_same_v<
MyArray<int, 2, 3>, std::array<std::array<int, 3>, 2>
>); // the dimension should be the opposite order
MyArray a; // a stacked 2-D array of size 2x3
a[0][2] = 2; // valid index
任何帮助将不胜感激!
我不知道如何只用一个using
来制作它;我能想象到的最好的需要辅助结构的帮助。
如下内容
template <typename, std::size_t ...>
struct MyArrayHelper;
template <typename T, std::size_t D0, std::size_t ... Ds>
struct MyArrayHelper<T, D0, Ds...>
{ using type = std::array<typename MyArrayHelper<T, Ds...>::type, D0>; };
template <typename T>
struct MyArrayHelper<T>
{ using type = T; };
template <typename T, std::size_t ... Ds>
using MyArray = typename MyArrayHelper<T, Ds...>::type;
相关文章:
- 检查输入 std::array 指针数据是否等于某个常量数组
- 基于 std::array 的多维数组
- std::array与C样式数组用于连续内存
- 具有静态 std::array 的神经网络比使用动态 C 数组的神经网络慢
- 不要声明 C 样式数组,而是使用 std::array<>
- 为什么我不能将 C 样式数组复制到 std::array?
- 创建 2D 数组类:如何访问 2D 数组类(如 array[x][y))中的元素
- 尝试使用 Array 类的模板构造整数数组时,错误:为什么数组类型"int [5]"不可分配,
- 为什么span的数组和std::array构造函数与其容器构造函数不同
- 键入 trait 以获取 std::array 或 C 样式数组的元素类型
- 原始数组和 std::array 在 clang++ 和 VC++ 上的不同迭代器行为
- 使用 std::array vs 数组传递对象数组
- 我无法将'2D array whit bool'转换为"空 2D 数组布尔值"(用于生命游戏)
- 为什么 C 数组比 std::array 快得多?
- 为什么在使用堆叠 std::array 声明多维数组时需要"双括号"?
- 使用索引数组订购 ArrayFire Array 的最佳方式
- NAPI AsyncWorker:将指针数组转换为NAPI::array,而不使用大的for
- 从std::array获取对原始数组的引用
- 获取 std::array 底层数组内存大小的最简单方法?
- 数组中的值之和返回内存地址,除非sizeof(array)/2