考虑以下声明:
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/