c++ - 查找对的匹配键范围
问题描述
假设我有一张地图:
std::map<std::pair<string,int>,int> map_example;
如何在我的地图中找到具有以下属性的键的所有元素: 对中的字符串是“A”,int 介于 5 和 10 之间,包括 5 和 10。或者,将我的地图制作成如下图会更好吗?
std::map<string,std::map<int,int> map_example2;
解决方案
该result
变量将为您提供一个map
包含map_example
符合您条件的所有元素:
decltype(map_example) result;
for (const auto& item : map_example) {
const auto& key = item.first;
if (key.first == "A" && key.second >= 5 && key.second <= 10)
result.insert(item);
}
它遍历整个map
并检查每个元素是否满足条件,如果是,则将元素插入到result
map
.
推荐阅读
- c# - ASP.NET Core 2.2 锁定未激活
- angular - 结构指令获取子指令
- javascript - 如何使用javascript从另一个文件中设置元素的innerHTML?
- c# - 将 IN 子句中的多个数字作为参数传递给 Oracle 查询时出现无效数字异常
- angular - gulp 错误:错误:找不到模块'@microsoft/sp-build-web'
- python - 如何选择 QTableWidget 的多个单元格?
- html - 为什么我的
出现在同一行的段落?
- python - python发生异常错误时播放声音
- javascript - TypeScript:为样式化的组件元素扩展 React 组件道具
- javascript - 为什么异步等待返回的承诺对象不是我自己的价值?