Iterators as associative containers' key

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ney André de Mello Zunino

    Iterators as associative containers' key

    Hello.

    A non-modifying algorithm I implemented uses two associative containers
    from STL: set and map. The elements on those containers are supposed to
    refer to actual elements which lie on another, bigger container.

    I had the definition of my auxiliary containers based on pointers, but
    considered using iterators instead. I know iterators might become
    invalid upon certain operations, but as I said, the algorithm is
    non-modifying. This is a snippet of what I was trying:

    set<int> s;
    typedef set<int>::itera tor ITER;

    s.insert(1);

    set<ITER> t;
    t.insert(s.begi n());

    In spite of my attempts, I could not get the code to work when using
    iterators as keys for the associative containers. This probably has
    good, obvious reasons, which are not clear to me at the moment. Would
    anybody be so kind to explain that which I am missing?

    Thank you,

    --
    Ney André de Mello Zunino

  • Victor Bazarov

    #2
    Re: Iterators as associative containers' key

    "Ney André de Mello Zunino" <zunino@unu.edu > wrote...[color=blue]
    > A non-modifying algorithm I implemented uses two associative containers
    > from STL: set and map. The elements on those containers are supposed to
    > refer to actual elements which lie on another, bigger container.
    >
    > I had the definition of my auxiliary containers based on pointers, but
    > considered using iterators instead. I know iterators might become
    > invalid upon certain operations, but as I said, the algorithm is
    > non-modifying. This is a snippet of what I was trying:
    >
    > set<int> s;
    > typedef set<int>::itera tor ITER;
    >
    > s.insert(1);
    >
    > set<ITER> t;
    > t.insert(s.begi n());
    >
    > In spite of my attempts, I could not get the code to work when using
    > iterators as keys for the associative containers. This probably has
    > good, obvious reasons, which are not clear to me at the moment. Would
    > anybody be so kind to explain that which I am missing?[/color]

    Set iterators do not define operator <, which is required if no
    other comparison functor is provided for the Key type. You could
    try defining your own comparison for type 'ITER' and pass it to
    the set<ITER, myComparisonTyp e>. The simplest I could imagine is

    struct myComparisonTyp e {
    bool operator()(cons t ITER& i1, const ITER& i2) const {
    return std::distance(i 1, i2) < 0;
    }
    };

    (or something like it). Try it, I used it and it seems to compile
    but will it work I am not sure.

    Victor


    Comment

    • Ney André de Mello Zunino

      #3
      Re: Iterators as associative containers' key

      Victor Bazarov wrote:
      [color=blue]
      > struct myComparisonTyp e {
      > bool operator()(cons t ITER& i1, const ITER& i2) const {
      > return std::distance(i 1, i2) < 0;
      > }
      > };
      >
      > (or something like it). Try it, I used it and it seems to compile
      > but will it work I am not sure.[/color]

      You are right and your solution works great. Thank you very much for the
      insight.

      Regards,

      --
      Ney André de Mello Zunino

      Comment

      Working...