Map-like container?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Pope

    #16
    Re: Map-like container?

    Victor Bazarov wrote:[color=blue]
    > Ben Pope wrote:[color=green]
    >> Sensei wrote:[color=darkred]
    >>> [..]
    >>> Better read that book pronto, since that & at the end of Cpp for the
    >>> ++ operator dazzles me :)[/color]
    >>
    >>
    >> It's a reference. A bit like a pointer, but different.[/color]
    >
    > Heh... "It's like a pointer, only it's not."[/color]

    Just trying to pique his curiosity without giving too much away ;)

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...

    Comment

    • Rolf Magnus

      #17
      Re: Map-like container?

      Sensei wrote:
      [color=blue]
      > On 2006-03-14 16:35:45 +0100, Artie Gold <artiegold@aust in.rr.com> said:
      >
      >[color=green]
      >> Sounds like a multimap to me. ;-)[/color]
      >
      >
      > Can you explain this?
      >
      > Following the SGI documentation, I understand it is possible to use
      > multimap with pairs, so the only way to achieve what I want is to be
      > tricky:
      >
      > multimap<int, int> adj;
      >
      > multimap<int, int>::iterator it_adj;
      >
      > /*
      > 1:
      > 2, 4, 1
      >
      > 2:
      > 6, 9, 4, 2, 1, 0
      > */
      >
      > adj.insert(pair <int,int>(1,2)) ;[/color]

      adj.insert(make _pair(1,2));
      [color=blue]
      > adj.insert(pair <int,int>(1,4)) ;
      > // ``2'' inserted before ending the key ``1''
      > adj.insert(pair <int,int>(2,6)) ;
      > adj.insert(pair <int,int>(2,9)) ;
      > adj.insert(pair <int,int>(2,4)) ;
      > adj.insert(pair <int,int>(2,1)) ;
      > adj.insert(pair <int,int>(2,0)) ;
      > // insert last ``1''
      > adj.insert(pair <int,int>(1,1)) ;
      > adj.insert(pair <int,int>(2,2)) ;
      >
      > it_adj = adj.find(1);
      >
      > while ((*it_adj).firs t == 1)[/color]

      Use equal_range instead:

      typedef multimap<int, int>::iterator iter;

      pair<iter, iter> b = adj.equal_range (1);
      for (iter it = b.first; it != b.second; ++it)
      cout << "that's 2 key and value " << (*it).second << endl;
      }
      [color=blue]
      > So I feel like there's no other way than this and a by-hand use of
      > map/lists... am I right? :)[/color]

      Comment

      Working...