vector find函数
时间: 2023-08-20 08:12:45 浏览: 121
测试Vector
vector的find函数是通过algorithm头文件中的find函数来实现的。在使用find函数时,需要包含<algorithm>头文件。例如,可以使用以下代码来查找vector中是否存在某个元素:
```cpp
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
vector<int>::iterator it = find(vec.begin(), vec.end(), 3);
if (it != vec.end()) {
cout << "found" << endl;
} else {
cout << "not found" << endl;
}
}
```
在这个例子中,我们使用find函数在vector中查找元素3。如果找到了,就输出"found",否则输出"not found"。注意,find函数返回的是一个迭代器,指向找到的元素的位置。如果没有找到,返回的是end()迭代器,表示到达了vector的末尾。\[1\]\[2\]
#### 引用[.reference_title]
- *1* [vector的find用法](https://blog.csdn.net/wcxyky/article/details/88543873)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [(c++)vector——find方法的使用](https://blog.csdn.net/LosGod/article/details/121181649)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [快速查找vector内的某一元素是否存在-find函数](https://blog.csdn.net/s20122502/article/details/106529330)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文