如何检查元素是否在 std::set 中?

2022-01-07 00:00:00 set contains c++ stl

如何检查元素是否在集合中?

How do you check that an element is in a set?

是否有更简单的等价于以下代码:

Is there a simpler equivalent of the following code:

myset.find(x) != myset.end()

推荐答案

在许多 STL 容器(例如 std::mapstd::set<)中检查是否存在的典型方法/code>, ... 是:

The typical way to check for existence in many STL containers such as std::map, std::set, ... is:

const bool is_in = container.find(element) != container.end();

相关文章