在C ++中,检查std :: vector< string>是否包含一定值 [英] In C++ check if std::vector<string> contains a certain value

查看:82
本文介绍了在C ++中,检查std :: vector< string>是否包含一定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 包括< algorithm>

这篇关于在C ++中,检查std :: vector&lt; string&gt;是否包含一定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆