How to get the inner most type of a n-nested vector?(如何获得n嵌套向量的最内部类型?)
问题描述
我需要获取 n 嵌套向量的内部类型.例如:
I need to get the inner type of a n-nested vector. For Example:
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
等等.我尝试使用包装器,但这会导致编译器错误.
and so on. I tried using a wrapper, but this results in a compiler error.
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.你只想要类型:
Your base case should be the non-vector case. For a non-vector, there is no ::value_type. You just want the type:
template <typename T>
struct base_type {
using type = T;
};
对于您的递归情况,base_type 的结果"类型被命名为 type.不是 value_type,所以我们必须在这里使用它:
For your recursive case, base_type's "result" type is named type. Not value_type, so we have to use that here:
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>
{ };
这篇关于如何获得n嵌套向量的最内部类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获得n嵌套向量的最内部类型?
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
