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.
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.
Use equal_range()
; it returns a pair of iterators describing the range of items with the specified key.
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;
}
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);
}