iterate through an STL multimap

Discussion in 'C++' started by Tommo, May 22, 2006.

  1. Tommo

    Tommo Guest

    Chaps,

    Could someone please provide me some examples of iterating through a
    multimap. I wish to do the following

    1. Iterate through each key in the multimap
    2. For each key i find get all the possible values out so I can check
    each value

    Cheers
     
    Tommo, May 22, 2006
    #1
    1. Advertising

  2. Tommo schrieb:
    > Chaps,
    >
    > Could someone please provide me some examples of iterating through a
    > multimap. I wish to do the following
    >
    > 1. Iterate through each key in the multimap
    > 2. For each key i find get all the possible values out so I can check
    > each value


    1. Iterate through all elements in the multimap<K,V>
    2. create a map<K, set<V> > and fill the set with all the values found

    Something along this:

    multimap<K, V> mm;
    map<K, vector<V> > values;

    for(it = mm.begin(); it != mm.end(); ++it
    {
    values[(*it).first].insert((*it).second);
    }

    HTH
    Stefan
    --
    Stefan Naewe
    naewe.s_AT_atlas_DOT_de
     
    =?ISO-8859-1?Q?Stefan_N=E4we?=, May 22, 2006
    #2
    1. Advertising

  3. Tommo

    Tom Widmer Guest

    Tommo wrote:
    > Chaps,
    >
    > Could someone please provide me some examples of iterating through a
    > multimap. I wish to do the following
    >
    > 1. Iterate through each key in the multimap
    > 2. For each key i find get all the possible values out so I can check
    > each value


    If you iterate from begin() to end(), you'll get the key-value pairs in
    order. You'll get each key multiple consecutive times, once for each of
    its values. Note that the values won't necessarily come in any
    particular order (e.g. not necessarily in insertion order).

    If this isn't particularly useful to you, you could use a
    map<key, vector<value> > or map<key, list<value> >
    instead.

    Tom
     
    Tom Widmer, May 22, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,140
    Sudsy
    Sep 4, 2003
  2. runescience
    Replies:
    0
    Views:
    1,494
    runescience
    Feb 9, 2006
  3. John Mills
    Replies:
    7
    Views:
    4,568
    John Harrison
    Jun 23, 2004
  4. Replies:
    3
    Views:
    509
    red floyd
    Sep 13, 2006
  5. John
    Replies:
    4
    Views:
    955
    RedGrittyBrick
    Apr 1, 2008
Loading...

Share This Page