Obtain container type from (its) iterator type in C++ (STL)(从 C++ (STL) 中的(它的)迭代器类型获取容器类型)
问题描述
给定一个容器来获取相关的迭代器很容易,例如:
It is easy given a container to get the associated iterators, example:
std::vector<double>::iterator i; //An iterator to a std::vector<double>
我想知道是否有可能,给定一个迭代器类型,推导出对应容器"的类型(这里我假设每个容器只有一个(非常量)迭代器).
I was wondering if it is possible, given an iterator type, to deduce the type of the "corresponding container" (here I am assuming that for each container there is one and only one (non-const) iterator).
更准确地说,我想要一个适用于所有 STL 容器的模板元函数(无需为每个容器手动专门化它),例如:
More precisely, I would like a template metafunction that works with all STL containers (without having to specialize it manually for each single container) such that, for example:
ContainerOf< std::vector<double>::iterator >::type
评估为
std::vector<double>
有可能吗?如果没有,为什么?
Is it possible? If not, why?
在此先感谢您的帮助!
推荐答案
我认为这不可能.在某些 STL 库中,您实际上有一个向量迭代器作为指针类型,即std::vector<T>::iterator 是一个 T* 所以我想不出有什么方法可以让你从那里回到容器类型.
I don't think this would be possible. On some STL libraries you actually have a vector iterator as a pointer type, i.e. std::vector<T>::iterator is a T* so I can't think of any way you could get back to the container type from that.
这篇关于从 C++ (STL) 中的(它的)迭代器类型获取容器类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 C++ (STL) 中的(它的)迭代器类型获取容器类型
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
