C++如何按值从std ::映射中删除项目?
我的地图:
std::map<std::array<byte, 16>, int> possible; // key is a byte array, value is a frequency of occurrence
我已经填写了地图。现在,我需要删除一次发生的阵列。有必要从地图上删除这样的对,其值等于一个。怎么做?
My map:
std::map<std::array<byte, 16>, int> possible; // key is a byte array, value is a frequency of occurrence
I already filled the map. Now I need to remove the arrays that occurred once. It is necessary to remove from the map such pairs, the value of which is equal to one. How to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 erase_if :
Use erase_if:
std :: Map
并非旨在按值逐值找到元素。因此,要按照您的要求,您将必须手动循环循环MAP
调用map :: erase()
在所需的项目上,例如:在C ++ 20及以后,您可以使用
std :: map
std :: erase_if()
,例如:std::map
isn't designed to find elements by value, only by key. So, to do what you are asking for, you will have to manually loop through themap
callingmap::erase()
on the desired items, eg:In C++20 and later, you can use the
std::map
overload ofstd::erase_if()
instead, eg: