如何檢查 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