获取指向迭代器引用的STL容器的指针? [英] Get a pointer to STL container an iterator is referencing?

查看:136
本文介绍了获取指向迭代器引用的STL容器的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,以下是可能的:

std::set<int> s;  
std::set<int>::iterator it = s.begin();

我想知道相反的情况是否可能,例如,

I wonder if the opposite is possible, say,

std::set<int>* pSet = it->**getContainer**();  // something like this...


推荐答案

不,那里

迭代器甚至可能没有引用容器。例如,对于两个 std,实现都可以使用 T * 作为 iterator 类型: array< T,N> std :: vector< T> ,因为它们都将其元素存储为数组。

An iterator may not even have a reference to the container. For example, an implementation could use T* as the iterator type for both std::array<T, N> and std::vector<T>, since both store their elements as arrays.

此外,迭代器比容器更通用,并且并非所有迭代器都指向容器(例如,有输入和输出迭代器可读取和写入流)。

In addition, iterators are far more general than containers, and not all iterators point into containers (for example, there are input and output iterators that read to and write from streams).

这篇关于获取指向迭代器引用的STL容器的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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