Get a pointer to STL container an iterator is referencing?

后端 未结 3 776
感动是毒
感动是毒 2020-12-11 01:06

For example, the following is possible:

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

I wonder if the opposite is

3条回答
  •  天涯浪人
    2020-12-11 01:44

    No, there is no portable way to do this.

    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 and std::vector, 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).

提交回复
热议问题