std::vector 判断是否包含
时间: 2023-09-10 17:04:50 浏览: 115
可以使用 `std::find` 函数判断一个 `std::vector` 是否包含某个元素,它返回一个指向该元素的迭代器,如果未找到该元素,则返回一个指向 `vector::end()` 的迭代器。例如:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
int target = 3;
auto it = std::find(vec.begin(), vec.end(), target);
if (it != vec.end()) {
std::cout << "vector contains " << target << "\n";
} else {
std::cout << "vector does not contain " << target << "\n";
}
return 0;
}
```
输出:
```
vector contains 3
```
相关问题
c++ 判断std::vector<std::string>是否包含指定关键字,请用c++代码说明.
好的,以下是示例代码:
```c++
#include <iostream>
#include <vector>
#include <algorithm> // for std::find
int main()
{
std::vector<std::string> keywords = {"hello", "world", "testing", "example"};
std::string search_word = "world";
// check if search_word is in keywords vector
if (std::find(keywords.begin(), keywords.end(), search_word) != keywords.end())
{
std::cout << "The keyword " << search_word << " has been found!" << std::endl;
}
else
{
std::cout << "The keyword " << search_word << " has not been found." << std::endl;
}
return 0;
}
```
这个程序会创建一个包含几个关键字的 `std::vector<std::string>` 对象,然后检查其中是否包含指定的关键字。通过使用 `std::find` 算法,它会在 `keywords` 向量中查找指定的关键字,并返回一个指向该关键字的迭代器。如果迭代器不等于向量的结尾迭代器,则表示找到了该关键字。否则,表示未找到该关键字。
c++ 判断类是否在vector中 std::find
在使用std::find函数判断类是否在vector中时,需要重载类的等于运算符(operator==)。
示例代码如下:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
class MyClass {
public:
int value;
MyClass(int val): value(val) {}
bool operator==(const MyClass& other) const {
return value == other.value;
}
};
int main() {
std::vector<MyClass> vec = {MyClass(1), MyClass(2), MyClass(3)};
MyClass target(2);
auto iter = std::find(vec.begin(), vec.end(), target);
if (iter != vec.end()) {
std::cout << "Found" << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
return 0;
}
```
在上述代码中,MyClass类包含一个int类型的成员变量value,并重载了等于运算符。通过std::find函数在vector中查找target对象,如果找到则输出“Found”,否则输出“Not found”。
需要注意的是,在重载等于运算符时需要考虑对象的所有成员变量,并确保在两个对象相等时返回true。否则可能会导致找不到目标对象的情况。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)