469,289 Members | 2,221 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

iterate through an STL multimap

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

May 22 '06 #1
2 12360
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
May 22 '06 #2
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
May 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Tanguy Fautré | last post: by
3 posts views Thread by He Shiming | last post: by
3 posts views Thread by Przemek | last post: by
1 post views Thread by placid | last post: by
4 posts views Thread by Nick Keighley | last post: by
4 posts views Thread by sks | last post: by
1 post views Thread by Saile | last post: by
1 post views Thread by ambarish.mitra | last post: by
20 posts views Thread by puzzlecracker | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.