在 C++ 中检查是否 std::vector<string>包含一定的价值

2021-12-21 00:00:00 vector c++ stdvector std

是否有任何内置函数告诉我我的向量是否包含某个元素例如

Is there any built in function which tells me that my vector contains a certain element or not e.g.

std::vector<string> v;
v.push_back("abc");
v.push_back("xyz");

if (v.contains("abc")) // I am looking for one such feature, is there any
                       // such function or i need to loop through whole vector?

推荐答案

您可以使用 <代码>std::find如下:

You can use std::find as follows:

if (std::find(v.begin(), v.end(), "abc") != v.end())
{
  // Element in vector.
}

为了能够使用std::find:include .

相关文章