Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

iterate through an STL multimap

5 views
Skip to first unread message

Tommo

unread,
May 22, 2006, 3:51:59 AM5/22/06
to
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

Stefan Näwe

unread,
May 22, 2006, 5:55:25 AM5/22/06
to
Tommo schrieb:

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

Tom Widmer

unread,
May 22, 2006, 6:17:57 AM5/22/06
to

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

0 new messages