I have std::multimap<string, MyObject*> dataMap;
where the keys are MyObject.name
and all MyObjects are stored in a std::vector<MyObject>
.
After filling the map I need to print the contents of dataMap
grouped by the same key, where I first need number of same keys with the help of dataMap.count(MyObject.name)
and then all the values with this key.
I was thinking of using two for loops
where the first loop iterates through "key group names" and counts all the keys that belong in this group, and the other for loop
iterates through all the keys in certain group and prints the MyObject.information
for(//iterate through group key names){
//print number of key occurences
for(//iterate through a certain group{
//print MyObject.information for all the keys in a group
}
}
The problem is, i don't really know how would implement this or rather how would I use iterators to my will. Any ideas?
EDIT: From the provided links i created this
for(std::multimap<string, MyObject*>::const_iterator itUnq = dataMap.cbegin();
itUnq != dataMap.cend(); itUnq = dataMap.upper_bound(itUnq->first)){
std::cout << dataMap.count(itUnq->second->name)
<< std::endl;
std::pair <std::multimap<string, MyObject*>::const_iterator,
std::multimap<string, MyObject*>::const_iterator> groupRange;
groupRange = dataMap.equal_range(itUnq->second->code);
//iterate through keys inside the group
for(std::multimap<string, MyObject*>::const_iterator itGroup = groupRange.first;
itGroup != groupRange.second; ++itGroup){
std::cout << itGroup->second->information
}
Comments?