从C ++(STL)中的(其)迭代器类型获取容器类型 [英] Obtain container type from (its) iterator type in C++ (STL)

查看:98
本文介绍了从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>



我想知道是否可能,给定一个迭代器类型,容器(这里我假设每个容器只有一个(非const)迭代器)。

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 



evaluates to

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)中的(其)迭代器类型获取容器类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆