Any possibility to obtain the container of an STL-iterator?
Hi,
does anyone know whether there exist any possibility to obtain (a pointer to) the container an STL-iterator refers to?
For example:
Consider many std::maps all of (e.g.) type std::map<int, float>.
Now you have an iterator of type std::map<int, float>::iterator which belongs to one of these maps and you want to know to which of them it refers to. Is there any possibility to find out (e.g. any function of the iterator which returns a pointer to the container)?
Thanks in advance,
Physicus.
Re: Any possibility to obtain the container of an STL-iterator?
Hi Physicus,
there´s no way to know what container the iterator belongs to. In the STL there´s a strict between separation between data (container) and algorithm (iterator).
Regards,
Guido
Re: Any possibility to obtain the container of an STL-iterator?
There may be a way to know if the container has a contiguous allocation or not.. but it doesn't buy you much.
Moreover, why do you want to compromise with the level of abstraction iterators provided for the containers used? If you want to know about the container, don't use iterators, use the containers themselves everywhere you want. But, this is not something elegant that you should do.
Why do you need to know about the container?
Re: Any possibility to obtain the container of an STL-iterator?
Instead of using iterators, use something like:
Code:
template <class Container>
struct IteratorInContainer {
Container* container;
typename Container::iterator iterator;
};
Quote:
Originally Posted by exterminator
Why do you need to know about the container?
Perhaps in order to erase/insert elements in the container.
Re: Any possibility to obtain the container of an STL-iterator?
Hi all,
I already expected that there is no possibility (because of the fact that containers and their iterators are strictly separated - as GNiewerth quite correctly mentioned).
Well, the reason why I was looking for is a long story. In short:
I have built an own container-class-template which in fact consists of a map of maps, each again contain maps with elements (principle: map<key1, map<key2, map<key3, value> > >). Now I also built an new iterator-class which allows to iterate through these folded maps in a distinct way (lets call it a super-iterator). This new iterator-class internally uses STL-map-iterators for example to iterate through the most inner maps. When I now want to compare two of these super-iterators it would be a bit more performant, when I could get the information to which container (inner map) the inner interator belongs to, just from the iterator alone.
But it's no problem if this possibility doesn't exist, because I have all informations within my super-iterator-class. It is only a question of performance, nothing else.
Thank you for your answers.
Best regards,
Physicus.