如何在C++中定义每个维度具有不同类型的 3D 数组
How to define a 3d array with each dimension a different type in C++?
我想定义一个这样的 3D 数组:
Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];
在C++可能吗?我正在使用Visual Studio 2010,不允许使用Boost库。如果可能,请告诉我如何初始化每个维度?
以下内容可能会对您有所帮助:
template <typename T, std::size_t N, typename IndexType>
class typed_array
{
public:
typedef typename std::array<T, N>::const_iterator const_iterator;
typedef typename std::array<T, N>::iterator iterator;
public:
const T& operator [] (IndexType index) const { return array[int(index)]; }
T& operator [] (IndexType index) { return array[int(index)]; }
// discard other index type
template <typename U> const T& operator [] (U&&) const = delete;
template <typename U> T& operator [] (U&&) = delete;
const_iterator begin() const { return array.begin(); }
const_iterator end() const { return array.end(); }
iterator begin() { return array.begin(); }
iterator end() { return array.end(); }
private:
std::array<T, N> array;
};
enum class E { A, B, C };
int main(int argc, char *argv[])
{
typed_array<int, 3, E> a;
typed_array<typed_array<int, 4, char>, 3, E> b;
//a[2] = 42; // doesn't compile as expected. `2` is not a `E`
b[E::A][' '] = 42;
//b[E::A][2] = 42; // doesn't compile as expected. `2` is not a `char`
return 0;
}
这是不可能的,因为它是同一类型的数组元素。
struct s_i {
int i;
const wchar *wc[7];
};
struct s_e {
enum Ex e;
struct s_i i[6];
} ary[3];
不,这是不可能的。
你可以做的是创建一个表现为枚举的类,并有一个下标运算符,该运算符返回另一个特殊类(通常表现为int
),该类具有下标运算符,该运算符又返回wchar_t
数组。
相关文章:
- C++默认情况下,指针类型数组的元素是否保证初始化为 nullptr?
- 类型数组?
- 如何在使用 make_unique<T[]>() 制作的模板类型数组上使用 std::fill?
- 将基本类型数组中的内存重新用于不同(但仍然是基本)类型的数组合法吗
- 在C++中返回基元类型数组(Java比较)
- 如何返回 QVariant 类型数组
- 如何在本机 c++ jni 中实现 java 类型数组
- 模板类型数组的模板类型数组
- 如何确定未知类型数组的索引是否为空
- 复杂类型数组的静态初始化
- 访问基类型数组成员(Int-to-type习惯用法)
- 如何将 char 类型数组传递给函数并将第一个字符数组复制到第二个图表数组并使用 c++ 进行比较
- 如何在C 中制作结构类型数组
- 如何创建结构类型数组的地图对象
- C++类类型数组
- C++中的Matlab类型数组
- 节点C++插件 - 当类型数组(Float32Array)作为参数传递时,如何访问它?
- 如何将二维字符类型数组逐行保存到 c 中的.txt文件中
- C++:用户定义的类型数组出错
- 在双精度类型数组中使用 memset()