c++vectorfind
时间: 2023-10-17 19:31:13 浏览: 89
在C++中,vector的find函数用于在一个vector容器中查找指定的元素。通过使用algorithm头文件中的find函数,可以对vector进行查找操作。下面是一个使用find函数在vector中查找元素的示例代码:
```cpp
#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;
}
```
在上述示例代码中,我们定义了一个名为vec的vector容器,并使用find函数在其中查找元素3。如果找到了元素,就会输出"找到了元素",否则输出"未找到元素"。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [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_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [1400集C C++全新企业核心项目攻坚 Ai项目+地震监测+婚恋+游戏+服务引擎 70G容量](https://download.csdn.net/download/qq_41972992/88270318)[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_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文