What's the most efficient way to check whether an stl vector of strings contains a specific string?
The obvious yet possibly too-slow solution is std::find(vec.begin(), vec.end(), your_string);
If your vector isn't changing much, sort it first, then use binary_search
, lower_bound
, upper_bound
, or equal_range
. If your vector changes a lot, consider using a set
/multiset
(or if needed map
/multimap
) instead.
Depending on your needs a hash (unordered_set
) might be appropriate as well, but it's more different from your initial container choice than normal ordered containers, and not supplied prior to C++0x (you can get it from boost easily).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With