c++ - 如何获得嵌套 vector 的维度(嵌套)(不是大小)?

标签 c++ vector

考虑以下声明:

vector<vector<int> > v2d;
vector<vector<vector<string>> > v3d;

如何在后续代码中找出 vector 的“维数”?例如,2 代表 v2d,3 代表 v3d?

最佳答案

以下几行:

template<class Y> 
struct s
{
    enum {dims = 0};
};

template<class Y>
struct s<std::vector<Y>>
{
    enum {dims = s<Y>::dims + 1};
};

那么例如,

std::vector<std::vector<double> > x;
int n = s<decltype(x)>::dims; /*n will be 2 in this case*/

具有吸引人的特性,即所有评估都在编译时

关于c++ - 如何获得嵌套 vector 的维度(嵌套)(不是大小)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28942758/

相关文章:

c++ - 错误 : passing 'const T' as 'this' argument of 'bool T::operator<(T)' discards qualifiers

c - OpenMP 并行乘法比顺序乘法慢

c++ - 从文件读入结构并添加到 vector

c++ - 使用#pragma detect_mismatch 确保 DLL 使用正确的静态链接库

c++ - 如何用 1 个变量替换正则表达式字符串?

c++ - 暴露复杂的短暂对象的正确方法

c++ - CCArray 与 std::vector

c - 在 C 中创建标量乘法函数

c++ - 如何在插入CRichEditCtrl时保持原始图像格式?

c++ - C++意外调用继承类的函数