是否有一种简单或标准的方法来使用多图迭代器迭代多图中的唯一键?
即对于一个看起来像的集合:{1, "a"}, {1, "lemon"}, {2, "peacock"}, {3, "angel"}
一个迭代器,它会在{1, "a"}然后开始递增,指向{2, "peacock"}然后再次递增会指向{3, "angel"}?
我有std::multimap<string, MyObject*> dataMap;密钥的位置MyObject.name,所有MyObjects都存储在一个std::vector<MyObject>.
在填充地图后,我需要打印dataMap由相同键分组的内容,其中我首先需要相同键的数量,dataMap.count(MyObject.name)然后使用此键的所有值.
我正在考虑使用两个for loops,其中第一个循环遍历"密钥组名称"并计算属于该组的所有密钥,另一个for loop循环遍历特定组中的所有密钥并打印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
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,我真的不知道如何实现这个,或者我将如何使用迭代器来实现我的意愿.有任何想法吗?
编辑:从提供的链接我创建了这个
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 …Run Code Online (Sandbox Code Playgroud) 例如,如果我有这样的mmap:
alice -> 30
bob -> 23
josh -> 20
josh -> 30
andy -> 40
andy -> 40
Run Code Online (Sandbox Code Playgroud)
只得到这对:
alice -> 30
bob -> 23
josh -> 20
andy -> 40
Run Code Online (Sandbox Code Playgroud)