CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2006
    Posts
    18

    Question 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.

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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
    - Guido

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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?

  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  5. #5
    Join Date
    Oct 2006
    Posts
    18

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured