c++ - 如何在 c++11 中使用 lamda 表达式过滤 std::map 键
问题描述
我在 std::map<std::string, std::string> 中有包含日期作为键和一些字节作为值的映射。例如
std::map<std::string , std::string> mapval = {{"2020-09-24", "1024"}, {"2020-09-25", "1024"},{"2020-09-26", "1024"},
{"2020-09-27", "1024"},{"2020-09-28", "1024"}};
我是否可以仅从给定的日期范围内检索密钥,例如如果我想要从当前日期起三天的数据,那么我将只获取“2020-09-24”、“2020-09-25”和 {“2020-09 -26”,“1024”}。我想要一个通用的,其中我有开始日期和结束日期,我将过滤该范围内的值和我将删除的其他值。可能吗 ?
解决方案
我不确定我是否完全理解这个问题,但也许这会很接近:
mapval = std::map<std::string , std::string>(
mapval.lower_bound(start_date),
mapval.upper_bound(end_date));
这将保留mapval
其键落在start_date
和end_date
(包括)之间的范围内的元素,并删除所有其他元素。
推荐阅读
- php - 检查有效值之间是否存在空值
- performance - 如何使用 CPLEX 减少 OPL 脚本中的计算时间?
- php - 单个自定义帖子类型的自定义管理员编辑页面
- python - 什么被设置为 np.unit8(array)
- r - 将英国各地学校的坐标映射到现有的 shapefile
- lua - 在 Lua 中解析和计算多行文本中的单词
- python - 由于断言错误,Pip 未安装 Pyserial
- swift - 在主(ContentView)SwiftUI 视图中以 UIViewRepresentable 的形式访问 MKMapView 元素
- c# - 在 C# 中返回任务的正确方法
- laravel-5 - Laravel/snappy 不显示 pdf 中的图像