c++ vector find函数
时间: 2023-10-15 16:24:07 浏览: 161
C++的vector类本身并没有提供find函数,但可以通过引入algorithm头文件来使用find函数。例如,可以使用以下代码来在vector中查找特定元素:
```
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::vector<int>::iterator it = std::find(vec.begin(), vec.end(), 3);
if (it != vec.end()) {
std::cout << "元素找到了" << std::endl;
} else {
std::cout << "元素未找到" << std::endl;
}
return 0;
}
```
以上代码首先创建了一个包含整数的vector,然后使用find函数在vector中查找值为3的元素。如果找到了该元素,就输出"元素找到了",否则输出"元素未找到"。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [(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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [C++ find函数详解](https://blog.csdn.net/weixin_46585382/article/details/125218856)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [老歪用Qt C++写的读取SEGY和SEGD格式的地震数据源码,支持波形显示和变密度显示 在Qt5.12版本上编译通过](https://download.csdn.net/download/Idealtracy/88244376)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文