C++ 如何遍历多重映射并打印按键分组的值?
我有C++ 如何遍历多重映射并打印按键分组的值?,c++,count,key,multimap,C++,Count,Key,Multimap,我有std::multimap dataMap其中键是MyObject.name,所有MyObject都存储在std::vector中 填充映射后,我需要打印按同一键分组的dataMap的内容,在dataMap.count(MyObject.name)的帮助下,首先需要相同键的数量,然后使用此键打印所有值 我正在考虑使用两个for循环,其中第一个循环遍历“密钥组名称”并统计属于该组的所有密钥,另一个for循环遍历特定组中的所有密钥并打印MyObject。信息 for(//iterate thro
std::multimap dataMap代码>其中键是MyObject.name
,所有MyObject都存储在std::vector
中
填充映射后,我需要打印按同一键分组的dataMap
的内容,在dataMap.count(MyObject.name)
的帮助下,首先需要相同键的数量,然后使用此键打印所有值
我正在考虑使用两个for循环
,其中第一个循环遍历“密钥组名称”并统计属于该组的所有密钥,另一个for循环
遍历特定组中的所有密钥并打印MyObject。信息
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
}
}
问题是,我真的不知道如何实现这一点,或者更确切地说,我将如何根据自己的意愿使用迭代器。有什么想法吗
编辑:从提供的链接我创建了这个
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
}
for(std::multimap::const_迭代器itUnq=dataMap.cbegin();
itUnq!=dataMap.cend();itUnq=dataMap.upper_bound(itUnq->first)){
std::cout second->name)
第二->代码);
//迭代组内的键
对于(std::multimap::const_迭代器itGroup=groupRange.first);
itGroup!=groupRange.second;++itGroup){
std::cout second->information
}
评论?根据我对您的问题的理解,您可以使用
有点像这样:
#include <map>
#include <ctime>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
struct MyObject
{
std::string name;
int information;
MyObject(const std::string& name, int information)
: name(name), information(information) {}
};
int main()
{
std::srand(std::time(0));
std::vector<MyObject> dataVec;
std::multimap<std::string, MyObject*> dataMap;
// Give each object a random letter
// between A-J as a name and some data
for(auto i = 0; i < 10; ++i)
dataVec.emplace_back(std::string(1, 'A' + std::rand() % 10), i);
// Fill dataMap from dataVec
for(auto&& data: dataVec)
dataMap.emplace(data.name, &data);
// Select the correct type for calling the equal_range function
decltype(dataMap.equal_range("")) range;
// iterate through multimap's elements (by key)
for(auto i = dataMap.begin(); i != dataMap.end(); i = range.second)
{
// Get the range of the current key
range = dataMap.equal_range(i->first);
// Now print out that whole range
for(auto d = range.first; d != range.second; ++d)
std::cout << d->first << ": " << d->second->information << '\n';
}
}
#包括
#包括
#包括
#包括
#包括
#包括
如果这不是你想要的,也许它仍然会给你解决具体问题的思路。什么是“所有的MyObject都存储在一个std::vector中”意思是?你还说dataMap
键是*MyObject
0U.e.只是一个指向MyObject的指针…还有一个潜在的内存泄漏,对吗?这意味着当我填充映射时,它是从向量读取的,其中键是MyObject.name
,值是对象本身。我使用了一个指针来避免复制对象。要非常小心-确保向量在指向其内容时不会改变-如果你向后推,向量可以移动东西如果你知道键很简单,请看相等的范围,如果没有其他问题-,这有助于澄清一些问题,谢谢:)@user2202368我没有注意到你在问题中发布了解决方案。唯一的问题是我所看到的真正不同之处在于,我的版本使用返回的equal_range()
来选择外部for循环中的下一个迭代器,这可能比再次计算上限()
要稍微有效一些。