c++ - 如何将迭代结果添加到 C++ 中的新多图?
问题描述
如何将结果插入新的多图?我正在尝试搜索字典容器以查找用户给出的关键字,并且我需要遍历 tempCollector 以查找不同的元素。但是,我似乎找不到将结果存储在 tempCollector 中的方法。
//current container with all the data
multimap<string, DictionaryItem> dictionaryContainer;
void search(vector<string> input) {
if (dictionaryContainer.empty()) {
cout << "it's empty";
}
int inputSize = input.size();
string tempKeyword = input[0];
//need to copy or insert the value and keys to the tempCollector
multimap<string, DictionaryItem>tempCollector;
//iteration to find keyword; want to store the result in tempCollector
auto its = dictionaryContainer.equal_range(tempKeyword);
for (multimap<string, DictionaryItem>::iterator itr =its.first; itr != its.second; itr++) {
itr->second.print();
}
};
解决方案
如果要复制整个its
范围:
tempCollector.insert(its.first, its.second);
如果要复制每个元素:
for (auto itr =its.first; itr != its.second; itr++) {
if (condition) {
tempCollector.emplace(tempKeyword, itr->second);
//or
tempCollector.insert(*itr);
//or
tempCollector.emplace(tempKeyWord, DictionaryItem(...));
}
}
请记住,(多)映射将键/值对处理为std::pair<Key,T>
(又名value_type
)。multimap::insert方法假定您已经构建了要插入的对,而
multimap ::emplace将为您构建它们。
推荐阅读
- asp.net-mvc - JSON 对象到 ASP.NET MVC
- python - 使用 Pandas 将嵌套的 JSON 转换为 CSV
- redis - 锁定redis集群
- c++ - 缺少#include 是否有可能在运行时破坏程序?
- php - array_push() 方法期望参数 1 是数组,但给出了 null
- css - 通过数组映射时生成网格容器样式
- angularjs - AngularJS - 获取.constant中定义的常量的值
- mongodb - MongoDB - 导出 - 导出与特定字段匹配的所有文档 - 在文件中列出
- amazon-web-services - 通过 terraform 部署 AWS WorkSpaces 时出现 ValidationException 错误
- android - 如何在android studio中存储选定的铃声?