如何检查 C++ 向量中元素是否存在
本文演示了多种方法来检查一个 C++ 向量中是否存在元素。
C++ std::find()
检查向量中是否存在元素
find
方法是 STL 算法库的一部分,它可以检查给定的元素是否存在于特定的范围内。该函数搜索一个等于用户传递的第三个参数的因子。相应的返回值是遍历到找到的第一个元素,如果没有找到,则返回范围的 end
。
注意,我们使用*
运算符来访问返回的字符串值,并在 if
语句中做一个比较条件,如下例所示。
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::find;
int main() {
string element_to_check1 = "nibble";
string element_to_check2 = "nimble";
vector<string> data_types = {"bit", "nibble",
"byte", "char",
"int", "long",
"long long", "float",
"double", "long double"};
if (*find(data_types.begin(), data_types.end(), element_to_check1) == element_to_check1) {
printf("%s is present in the vector\n", element_to_check1.c_str());
} else {
printf("%s is not present in the vector\n", element_to_check1.c_str());
}
if (*find(data_types.begin(), data_types.end(), element_to_check2) == element_to_check2) {
printf("%s is present in the vector\n", element_to_check2.c_str());
} else {
printf("%s is not present in the vector\n", element_to_check2.c_str());
}
return EXIT_SUCCESS;
}
输出:
nibble is present in the vector
nimble is not present in the vector
C++ 基于范围的 for
循环来检查元素是否存在于向量中
基于范围的 for
循环可以作为另一种解决方案来检查给定元素是否存在于向量中。这种方法比较直接,因为它遍历向量;每次遍历都检查是否与给定的字符串相等。如果一个元素匹配,就会打印一个确认字符串,然后使用 break
语句停止循环。
#include <iostream>
#include <vector>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
int main() {
string element_to_check = "nibble";
vector<string> data_types = {"bit", "nibble",
"byte", "char",
"int", "long",
"long long", "float",
"double", "long double"};
for (const auto &item : data_types) {
if (item == element_to_check) {
printf("%s is present in the vector\n", element_to_check.c_str());
break;
}
}
return EXIT_SUCCESS;
}
输出:
nibble is present in the vector
C++ any_of()
检查向量中是否存在元素
<algorithm>
头文件的另一个有用的方法是 any_of
算法,它与 find
方法类似。any_of
方法检查作为第三个参数指定的单元谓词是否对给定范围内的至少一个元素返回 true
。在这个例子中,我们使用一个 lambda 表达式来构建一个用于比较向量元素的单元谓词。
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::any_of;
int main() {
string element_to_check = "nibble";
vector<string> data_types = {"bit", "nibble",
"byte", "char",
"int", "long",
"long long", "float",
"double", "long double"};
if (any_of(data_types.begin(), data_types.end(), [&](const string& elem) { return elem == element_to_check; })) {
printf("%s is present in the vector\n", element_to_check.c_str());
}
return EXIT_SUCCESS;
}
输出:
nibble is present in the vector
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn