C++ 基于std::array的多维数组初始化
在看到std::array的优点之后,我试图创建一个支持多维的类 我最初的实验使用嵌套的std::array。我之所以选择不使用这种方法,部分原因是因为我写这种类型的方式很难看 ie:C++ 基于std::array的多维数组初始化,c++,C++,在看到std::array的优点之后,我试图创建一个支持多维的类 我最初的实验使用嵌套的std::array。我之所以选择不使用这种方法,部分原因是因为我写这种类型的方式很难看 ie:std::array 除了初始化之外,新类大部分都在工作。我还没有决定是最好使用继承还是包含。选择可能取决于我是否能使初始化工作 如何编译此文件的最后两行: // multi-dimensional array based on std::array #include <array> templat
std::array
除了初始化之外,新类大部分都在工作。我还没有决定是最好使用继承还是包含。选择可能取决于我是否能使初始化工作
如何编译此文件的最后两行:
// multi-dimensional array based on std::array
#include <array>
template <class T, int s, int... r>
class arraynd_a : public std::array<arraynd_a<T, r...>, s>
{
public:
};
template <class T, int s>
class arraynd_a<T, s> : public std::array<T, s>
{
public:
};
template <class T, int s, int... r>
class arraynd_b
{
public:
std::array<arraynd_b<T, r...>, s> arr;
};
template <class T, int s>
class arraynd_b<T, s>
{
public:
std::array<T, s> arr;
};
void test()
{
constexpr std::array<std::array<int, 2>, 3> a1 = { { { 0, 1 }, { 1, 0 }, { 2, 4 } } };
/*constexpr*/ arraynd_a<int, 3, 2> a2a;
/*constexpr*/ arraynd_b<int, 3, 2> a2b;
#if 0
/*constexpr*/ arraynd_a<int, 3, 2> a3a = { { { 0, 1 }, { 1, 0 }, { 2, 4 } } };
#endif
#if 0
/*constexpr*/ arraynd_b<int, 3, 2> a3b = { { { 0, 1 }, { 1, 0 }, { 2, 4 } } };
#endif
}
//基于std::array的多维数组
#包括
模板
类arraynd_a:public std::array
{
公众:
};
模板
类arraynd_a:public std::array
{
公众:
};
模板
类arraynd_b
{
公众:
std::阵列arr;
};
模板
类arraynd_b
{
公众:
std::阵列arr;
};
无效测试()
{
constexpr std::数组a1={{{0,1},{1,0},{2,4}};
/*constexpr*/arraynd_a a2a;
/*constexpr*/arraynd_b a2b;
#如果0
/*constexpr*/arraynd_a a3a={{{0,1},{1,0},{2,4}};
#恩迪夫
#如果0
/*constexpr*/arraynd_b a3b={{{0,1},{1,0},{2,4}};
#恩迪夫
}
如果您使用的是成员类方式,则必须使用{}再次包装数组内容(也不能使用std::array arr=1、2;
或?)初始化数组:
看起来这还没有实现。在旧的标准中,有一个条款明确禁止聚合类具有基类:我能够保留我的原始设计并从本机多维数组初始化它:
#include <array>
template <class T, size_t s, size_t...r>
struct arraynd
{
typedef arraynd<T, r...> inner_type;
typedef typename inner_type::native_type native_type[s];
static constexpr std::array<inner_type, s> to_arraynd(const native_type& init)
{
return impl_to_arraynd(init, std::make_index_sequence<s> {});
}
template <std::size_t... I>
static constexpr std::array<inner_type, s> impl_to_arraynd(const native_type& init, std::index_sequence<I...>)
{
return { inner_type(init[I])... };
}
constexpr arraynd()
{
}
constexpr arraynd(const native_type& init)
: inner(to_arraynd(init))
{
}
std::array<inner_type, s> inner;
};
template <class T, size_t s>
struct arraynd<T, s>
{
typedef T inner_type;
typedef T native_type[s];
static constexpr std::array<inner_type, s> to_arraynd(const native_type& init)
{
return impl_to_arraynd(init, std::make_index_sequence<s> {});
}
template <std::size_t... I>
static constexpr std::array<inner_type, s> impl_to_arraynd(const native_type& init, std::index_sequence<I...>)
{
return { inner_type(init[I])... };
}
constexpr arraynd()
{
}
constexpr arraynd(const native_type& init)
: inner(to_arraynd(init))
{
}
std::array<inner_type, s> inner;
};
int main()
{
constexpr int a2native[2][3] = { { 1, 2, 3 }, { 1, 2, 3 } };
constexpr std::array<std::array<int, 3>, 2> a2std = { { { 1, 2, 3 }, { 1, 2, 3 } } };
constexpr arraynd<int, 2, 3> a2({ { 1, 2, 3 }, { 1, 2, 3 } });
return 0;
}
#包括
模板
结构阵列
{
类型定义阵列和内部_类型;
typedef typename internal_type::native_type native_type[s];
静态constexpr std::数组到_arraynd(const native_type&init)
{
将impl_返回给_arraynd(init,std::make_index_sequence{});
}
模板
静态constexpr std::array impl_to_arraynd(const native_type&init,std::index_序列)
{
返回{inner_type(init[I])…};
}
constexpr arraynd()
{
}
constexpr arraynd(const native_type和init)
:内部(至_arraynd(初始))
{
}
std::数组内部;
};
模板
结构阵列
{
类型定义T内螺纹类型;
typedef T native_type[s];
静态constexpr std::数组到_arraynd(const native_type&init)
{
将impl_返回给_arraynd(init,std::make_index_sequence{});
}
模板
静态constexpr std::array impl_to_arraynd(const native_type&init,std::index_序列)
{
返回{inner_type(init[I])…};
}
constexpr arraynd()
{
}
constexpr arraynd(const native_type和init)
:内部(至_arraynd(初始))
{
}
std::数组内部;
};
int main()
{
constexpr int a2native[2][3]={{1,2,3},{1,2,3};
数组a2std={{{1,2,3},{1,2,3}};
constexpr arraynd a2({1,2,3},{1,2,3});
返回0;
}
如果您担心嵌套数组的类型难看,请考虑使用TyPulf或使用声明。访问数组为:<代码> A2B.ARR= {…} /代码>执行该作业,但我不能用聚合初始化来解决这个问题,我建议您查看一个库,如EGEN或AAMDRILO。你不必重新发明它<代码> STD::数组< /代码>的大小参数是 STD::SigeZyt不是<代码> int >代码>,请考虑使用它,谢谢,因此,在我原来的问题的形式中,答案是更多的括号:ARARYNDB B A3B= {{{{{{ 0, 1 }}},{{{ 1, 0 }}},{{{ 2, 4 }}}}};因此,arr_mult_dim_t arr_3的类型实际上是std::array,它只是使用嵌套的using声明定义的。关于支架,这看起来确实更好。@RadAd是的,没错
arr_mult_dim_t<int, 2> arr_1 = { { 0, 1 } };
template <class T, std::size_t DIM, std::size_t... ARGS>
struct arr_mult_dim
{
using type = std::array<typename arr_mult_dim<T, ARGS...>::type, DIM>;
};
template <class T, std::size_t DIM>
struct arr_mult_dim<T, DIM>
{
using type = std::array<T, DIM>;
};
template <class T, std::size_t... DIMS>
using arr_mult_dim_t = typename arr_mult_dim<T, DIMS...>::type;
arr_mult_dim_t<int, 2> arr_1 = { 0, 1 };
arr_mult_dim_t<int, 2, 2> arr_2 = { { { 0, 1 }, {0, 1} } };
arr_mult_dim_t<int, 2, 2, 2> arr_3 =
{
{
{
{
{0, 1 },
{ 0, 1 }
}
},
{
{
{ 0, 1 },
{ 0, 1 }
}
}
}
};
The elements of an aggregate are: ...
- for a class, the direct base classes in declaration order followed by the direct non-static data members
in declaration order.
#include <array>
template <class T, size_t s, size_t...r>
struct arraynd
{
typedef arraynd<T, r...> inner_type;
typedef typename inner_type::native_type native_type[s];
static constexpr std::array<inner_type, s> to_arraynd(const native_type& init)
{
return impl_to_arraynd(init, std::make_index_sequence<s> {});
}
template <std::size_t... I>
static constexpr std::array<inner_type, s> impl_to_arraynd(const native_type& init, std::index_sequence<I...>)
{
return { inner_type(init[I])... };
}
constexpr arraynd()
{
}
constexpr arraynd(const native_type& init)
: inner(to_arraynd(init))
{
}
std::array<inner_type, s> inner;
};
template <class T, size_t s>
struct arraynd<T, s>
{
typedef T inner_type;
typedef T native_type[s];
static constexpr std::array<inner_type, s> to_arraynd(const native_type& init)
{
return impl_to_arraynd(init, std::make_index_sequence<s> {});
}
template <std::size_t... I>
static constexpr std::array<inner_type, s> impl_to_arraynd(const native_type& init, std::index_sequence<I...>)
{
return { inner_type(init[I])... };
}
constexpr arraynd()
{
}
constexpr arraynd(const native_type& init)
: inner(to_arraynd(init))
{
}
std::array<inner_type, s> inner;
};
int main()
{
constexpr int a2native[2][3] = { { 1, 2, 3 }, { 1, 2, 3 } };
constexpr std::array<std::array<int, 3>, 2> a2std = { { { 1, 2, 3 }, { 1, 2, 3 } } };
constexpr arraynd<int, 2, 3> a2({ { 1, 2, 3 }, { 1, 2, 3 } });
return 0;
}