如何获取n嵌套向量的最内部类型
How to get the inner most type of a n-nested vector?
我需要得到一个n嵌套向量的内部类型。例如:
type a; //base_type of a = type
std::vector<type> b; //base_type of b = type
std::vector<std::vector<type>> c;//base_type of c = type
等等。我尝试使用包装器,但这导致了编译器错误。
template<typename T1>
struct base_type : T1::value_type { };
template<typename T1>
struct base_type<std::vector<T1>> {
using type = typename base_type<T1>::value_type;
};
您的两种情况都是错误的。
您的基本情况应该是非vector
情况。对于非vector
,不存在::value_type
。你只需要类型:
template <typename T>
struct base_type {
using type = T;
};
对于递归情况,base_type
的"结果"类型命名为type
。不是value_type
,所以我们必须在这里使用它:
template<typename T>
struct base_type<std::vector<T>> {
using type = typename base_type<T>::type;
};
我们可以简化为:
template<typename T>
struct base_type<std::vector<T>>
: base_type<T>
{ };
相关文章:
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 选择和修改嵌套向量中的条目的最佳实践
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- 在C++中生成任意嵌套的向量
- 从列表/向量制作嵌套 for 循环的最佳方法是什么?
- 嵌套向量的缺点是什么?
- 如何通过迭代器访问向量中的嵌套对?
- 嵌套列表(字符串向量的向量)初始化失败
- 如何使用嵌套初始化构造函数中的一维向量初始化矩阵
- 删除嵌套向量中具有remove_if的元素
- 在循环访问嵌套向量时删除元素
- 使用嵌套循环搜索两个向量并查看其属性
- 如何在 C++ 中将结构类型向量数据输入到结构向量成员(嵌套结构向量)中
- C++:清除嵌套向量会导致奇怪的结果
- 如何在嵌套列表初始化中从map<><>中辨别向量?
- 类字符串和嵌套向量C
- 如何在C 中打印出嵌套向量的内容
- 如何在C++中生成任意嵌套的向量
- 使用嵌套向量动态分配方阵
- 在嵌套向量C 中获取特定元素