在C ++中检查std :: vector <string>是否包含某个值
是否有任何内置函数告诉我,我的向量包含某个元素或没有,例如
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 ,如下所示: 
 if (std::find(v.begin(), v.end(), "abc") != v.end()) { // Element in vector. } 
 为了能够使用std::find : include <algorithm> 。 
- 
如果您的容器只包含唯一值,请考虑使用 std::set。 它允许查询具有对数复杂度的集合成员资格。std::set<std::string> s; s.insert("abc"); s.insert("xyz"); if (s.find("abc") != s.end()) { ...
- 
如果你的向量保持sorting,使用 std::binary_search,它也提供对数复杂度。
- 
如果一切都失败了,回退到 std::find,这是一个简单的线性search。
在C ++ 11中,您可以使用std :: any_of来代替。
 它在<algorithm>并被称为std::find 。 
  std::find() 。