C++ 标准库 - <set>

C++ std::set::find 查找等于给定值的元素。

原型

iterator find(const key_type& k);
const_iterator find(const key_type& k) const;

参数

  • k:要找的值。

返回值

指向该元素的迭代器,或 end()。

示例

#include <iostream>
#include <set>

int main() {
    std::set<int> s{1, 3, 5};
    if (s.find(3) != s.end()) {
        std::cout << "yes\n";
    }
    if (s.find(2) == s.end()) {
        std::cout << "no\n";
    }
    return 0;
}

输出:

yes
no

注意事项

不要对 end() 解引用。只需判断存在时也可用 count。

下一页介绍count。