向量。检查它是否包含"Key" .C++

Vector. Checking to see if it contains "Key". C++

本文关键字:Key C++ 包含 是否 检查 向量      更新时间:2023-10-16

可能的重复项:
如何在 std::vector 中找到一个项目?

嘿,就像标题建议我想检查向量是否包含字符串"Key"。 我一直在谷歌上四处寻找,我在矢量库中找不到任何东西。 任何人都可以帮我解决这个问题。 提前谢谢。

您可以使用

std::find。假设您有一个充满std::stringsstd::vector

#include <algorithm> // for std::find
std::vector<std::string> v = ....;
std::vector<std::string>::const_iterator it = std::find(v.begin(), v.end(), "Key");
bool found = it != v.end();