在 unordered_map 中查找值
我正在使用 Boost unordered_map。我为每个条目都有一个键值对。如何确定地图中是否存在特定值? (我不想创建另一个将值存储为键并将键存储为值的 unordered_map )
谢谢。
I am using Boost unordered_map. I have a key value pair for each entry. How could I determine whether a particular value exist in the map? (I don't want to create another unordered_map which stored the value as key and key as value)
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
或者使用捕获的外部变量:
How about the following:
Or using an external variable that is captured:
Boost 有 Bimap,它是双向映射(即键和值都互相引用)。这听起来比
unordered_map
更适合您的需求。Boost has the Bimap, which is a bidirectional map (ie, keys and values both refer to each other). This sounds more appropriate for your needs than the
unordered_map
.您需要迭代
unordered_map
中的所有元素并查看给定值是否存在。带有自定义谓词的
std::find_if
算法可用于简化此操作。You need to iterate over all the elements in the
unordered_map
and see if the given value exists.The
std::find_if
algorithm with a custom predicate can be used to simplify this.