c++set find
时间: 2023-08-06 18:11:20 浏览: 41
在 C++ 中,`std::set` 是一个关联容器,它存储唯一且已排序的元素集合。`std::set::find()` 函数用于查找给定元素在 set 中的位置。如果元素存在,则返回指向该元素的迭代器;否则返回 `std::set::end()`。
下面是一个示例:
```c++
#include <iostream>
#include <set>
int main() {
std::set<int> s = {1, 2, 3, 4, 5};
// 查找元素 3 的位置
auto it = s.find(3);
if (it != s.end()) {
std::cout << "Found element: " << *it << std::endl;
} else {
std::cout << "Element not found!" << std::endl;
}
// 查找元素 6 的位置
it = s.find(6);
if (it != s.end()) {
std::cout << "Found element: " << *it << std::endl;
} else {
std::cout << "Element not found!" << std::endl;
}
return 0;
}
```
输出结果:
```
Found element: 3
Element not found!
```