gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:40:23 32 4
gpt4 key购买 nike

考虑以下声明:

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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com