c++ vector的find函数用法
时间: 2023-10-14 16:26:41 浏览: 64
C++ 的 vector 类型提供了 find() 函数,用于在 vector 中查找指定元素。
find() 函数的语法如下:
```
iterator find (iterator first, iterator last, const T& val);
```
其中,first 和 last 是表示要查找的元素所在的范围的迭代器,val 是要查找的元素。
如果找到了指定元素,则函数返回该元素的迭代器;否则,函数返回 last 迭代器。
下面是一个示例:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> vec {1, 2, 3, 4, 5};
// 查找元素 3 是否在 vector 中
auto it = find(vec.begin(), vec.end(), 3);
if (it != vec.end()) {
cout << "元素 3 在 vector 中" << endl;
} else {
cout << "元素 3 不在 vector 中" << endl;
}
return 0;
}
```
输出结果为:
```
元素 3 在 vector 中
```
相关问题
C++ vector find函数
C++中的vector并没有自带的find函数,但可以通过algorithm库中的find函数来实现。具体使用方法如下:
```cpp
#include <vector>
#include <algorithm>
#include <iostream>
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 << "Found" << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
return 0;
}
```
上述代码中,我们首先定义了一个vector,然后使用std::find函数在其中查找元素3。如果找到了,就输出“Found”,否则输出“Not found”。
c++vectorfind函数
C++的vector类中的find()函数是用来查找指定元素的方法。它可以在vector中搜索指定元素,并返回一个迭代器指向该元素的位置。这个函数需要包含<algorithm>头文件才能使用。使用find()函数的方法是首先定义一个迭代器,并将其初始化为find()函数的返回值。然后通过判断迭代器是否指向vector的末尾来确定是否找到了指定元素。如果找到了元素,可以通过迭代器获取该元素的值,以及通过迭代器与vector的起始迭代器相减来获取该元素在vector中的下标位置。
下面是一个示例代码,演示了如何使用vector的find()函数来查找元素7的位置:
```
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> arr;
arr.push_back(1);
arr.push_back(5);
arr.push_back(12);
arr.push_back(7);
arr.push_back(6);
arr.push_back(4);
std::vector<int>::iterator it;
it = std::find(arr.begin(), arr.end(), 7);
if (it != arr.end()) {
std::cout << "找到了元素:" << *it << std::endl;
std::cout << "元素的下标位置:" << it - arr.begin() << std::endl;
} else {
std::cout << "未找到元素" << std::endl;
}
return 0;
}
```
输出结果:
```
1 5 12 7 6 4
找到了元素:7
元素的下标位置:3
```
在上述示例代码中,我们创建了一个vector arr,并使用push_back()函数向其中添加了一些元素。然后,我们使用find()函数查找元素7的位置。如果找到了元素,输出该元素的值以及在vector中的下标位置。如果未找到元素,则输出未找到的信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)