105

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?
jogojapan
  • 68,383
  • 11
  • 101
  • 131
Jame
  • 21,150
  • 37
  • 80
  • 107
  • http://stackoverflow.com/questions/507884/what-is-the-nicest-way-to-find-a-specific-string-in-vector – Ben Nov 08 '12 at 02:28
  • 8
    As of C++11, this question is no longer a duplicate, as `std::any_of` now gives you a specific way to return true if a container CONTAINS a match, instead of using std::find to return a matching element. Thanks to @colddie for the original correct answer to this. – Nanki Jun 15 '15 at 09:47
  • Related: https://stackoverflow.com/a/31933118/8781554 – Xam Mar 08 '18 at 00:46

5 Answers5

230

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>.

AVH
  • 11,349
  • 4
  • 34
  • 43
  • And what if the thing I'm trying to search is `argv`, which isn't a vector or an array (I'm trying to see if a certain command line argument option is present in the passed `argv`)? I created a vector containing all the "elements" of `argv` and applied your solution, though I'm not sure how clean that is. – pretzlstyle Nov 29 '17 at 22:45
  • 1
    @jphollowed `std::string const searchStr = "magic"; auto result = std::find_if(argv + 1, argv + argc, [&](char const * const arg) { return arg == searchStr; });` works. Not as clean and concise though. You'll need at least C++11 for that to compile. – AVH Nov 30 '17 at 07:56
43
  1. If your container only contains unique values, consider using std::set instead. It allows querying of set membership with logarithmic complexity.

     std::set<std::string> s;
     s.insert("abc");
     s.insert("xyz");
     if (s.find("abc") != s.end()) { ...
    
  2. If your vector is kept sorted, use std::binary_search, it offers logarithmic complexity as well.

  3. If all else fails, fall back to std::find, which is a simple linear search.

William Miller
  • 9,839
  • 3
  • 25
  • 46
Alex B
  • 82,554
  • 44
  • 203
  • 280
  • 4
    Even better, if you don't need an ordering on the strings, use a `std::tr1::unordered_set`, available from `` or ``, which has (almost) constant lookup and query time. With either set or unordered_set you can alternatively say `if (s.count("abc"))`. Don't forget to _accept_ one of the answers. – Kerrek SB Jun 08 '11 at 11:05
  • All three of these links seem to link to now-archived pages. – glhrmv Apr 09 '18 at 15:32
24

In C++11, you can use std::any_of instead.

An example to find if there is any zero in the array:

std::array<int,3> foo = {0,1,-1};
if ( std::any_of(foo.begin(), foo.end(), [](int i){return i==0;}) )
std::cout << "zero found...";
colddie
  • 1,029
  • 1
  • 15
  • 28
  • I find any_of much better than find, because find return an vector iterator to the element, and any_of return a boolean and much more suitable to if cases – TripleS Jan 11 '17 at 10:13
  • 1
    Can you add some more detail? An example of how to use and a link to the documentation. – j b Dec 05 '17 at 11:20
  • @JamieBullock have you seen this [answer](https://stackoverflow.com/a/31933118/8781554)? – Xam Mar 08 '18 at 00:44
  • 3
    @Xam Thanks. I know how to use `any_of`, I was encouraging colddie to improve the answer – j b Mar 08 '18 at 08:20
6

it's in <algorithm> and called std::find.

Nim
  • 33,299
  • 2
  • 62
  • 101
3

std::find().

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680