Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ concisely checking if item in STL container (e.g. vector)

Tags:

c++

stl

boost

bool xInItems = std::find(items.begin(), items.end(), x) != items.end();

Is there a more concise way of checking if x is in items? This seems unnecessarily verbose (repeating items three times), which makes the intent of the code a little harder to read.

For example, is there something like the following:

bool xInItems = boost::contains(items, x);

If there doesn't exist any more concise boost/stl algorithm to check if a collection contains an item, is it considered good or bad practice to use instead a helper function to enable contains(items, x)?

Am I using the wrong STL container? Even a std::set would result in bool xInItems = items.find(x) != items.end(); which still seems verbose. Am I thinking about this the wrong way?

like image 468
JDiMatteo Avatar asked Oct 03 '14 20:10

JDiMatteo


3 Answers

It's not hard to write a template function from scratch.

template<typename T, typename Iterator>
bool contains(Iterator it1, Iterator it2, const T & value)
{
    return std::find(it1, it2, value) != it2;
}

template<typename T, typename Container>
bool contains(const Container & c, const T & value)
{
    return contains(c.begin(), c.end(), value);
}

You can even provide specializations for containers that have their own find function so that it's not calling std::find.

like image 126
Mark Ransom Avatar answered Sep 19 '22 17:09

Mark Ransom


any_of_equal will do the task:

#include <boost/algorithm/cxx11/any_of.hpp>

bool xInItems = boost::algorithm::any_of_equal(items, x);
like image 32
Mikhail Avatar answered Sep 17 '22 17:09

Mikhail


If your data is sorted, you can use std::binary_search, which returns a bool:

bool xInItems = std::binary_search(items.begin(), items.end(), x));

If you really need to leave the items un-sorted, but have C++11 available, you could use std::any_of, but it requires a predicate, so it's likely to end up at least as verbose as std::find (and probably more so).

like image 45
Jerry Coffin Avatar answered Sep 20 '22 17:09

Jerry Coffin