iterate through an STL multimap

T

Tommo

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
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Tommo said:
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
 
T

Tom Widmer

Tommo said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top