0

Possible Duplicate:
stl::multimap - how do i get groups of data?

What i want to do is to compute the list of values of every key stored in the multimap.

Community
  • 1
  • 1
Marco Masci
  • 818
  • 10
  • 22
  • What have you tried? Have you even done any basic research into how to use a `multimap`? – Jonathan Wakely Jun 23 '12 at 10:51
  • Have you understood my question? If you know the answer i would be pleased to hear... – Marco Masci Jun 23 '12 at 10:54
  • Yes, I know the answer, but I'm not answering it because I think it's a lazy, poorly-researched question. It would be easy for you to find an answer yourself, if you made any effort. Google for "stl multimap" and you get how to iterate on a multimap as the **very first result** http://www.sgi.com/tech/stl/Multimap.html – Jonathan Wakely Jun 23 '12 at 10:57
  • Wrong answer. You should read more carefully the question ( early, i have editated the title in order to express it more clearly). Consider that i do not know the keys... – Marco Masci Jun 23 '12 at 11:15
  • 1
    That's why you should _iterate_. The example on the page I linked to shows how to print all the keys and values. Hmm, how could that possibly be helpful to get a list of all the keys and values? – Jonathan Wakely Jun 23 '12 at 11:17

3 Answers3

2

Use equal_range(); it returns a pair of iterators describing the range of items with the specified key.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
2

A generic answer to the generic question is:

template<class KEY, class VALUE>
std::vector<VALUE> getValues(const std::multimap<KEY,VALUE>& aMap){
    std::vector<VALUE> values;
    for(multimap<KEY,VALUE>::const_iterator it=aMap.begin(), end=aMap.end();it!=end;++it){
        values.push_back((*it).second);
    }
    return values;
}
quamrana
  • 37,849
  • 12
  • 53
  • 71
0

Something like that should work

multimap<string, int> m;
vector<int> values;

for (multimap<string, int>::iterator it = m.begin(); it != m.end(); ++it)
{
    values.push_back((*it).second);
}