Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++ check if std::vector<string> contains a certain value [duplicate]

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? 
like image 503
Jame Avatar asked Jun 08 '11 10:06

Jame


1 Answers

You can use std::find as follows:

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

To be able to use std::find: include <algorithm>.

like image 131
AVH Avatar answered Sep 28 '22 22:09

AVH