This question already has answers here: How to find out if an item is present in a std::vector? (18 answers) Closed 2 years ago.
Is there something in <algorithm>
which allows you to check if a std:: container contains something? Or, a way to make one, for example:
if(a.x == b.x && a.y == b.y)
return true;
return false;
Can this only be done with std::map
since it uses keys?
Thanks
<algorithm>
- note no .h.
operator==
to compare them; then std::find
will work.
boost::algorithm::any_of_equal
.
Checking if v
contains the element x
:
#include <algorithm>
if(std::find(v.begin(), v.end(), x) != v.end()) {
/* v contains x */
} else {
/* v does not contain x */
}
Checking if v
contains elements (is non-empty):
if(!v.empty()){
/* v is non-empty */
} else {
/* v is empty */
}
If searching for an element is important, I'd recommend std::set
instead of std::vector
. Using this:
std::find(vec.begin(), vec.end(), x)
runs in O(n) time, but std::set
has its own find()
member (ie. myset.find(x)
) which runs in O(log n) time - that's much more efficient with large numbers of elements
std::set
also guarantees all the added elements are unique, which saves you from having to do anything like if not contained then push_back()...
.
set
have a count
method like map
? I also want to be able to get the index of the element in a set.
set
doesn't work like a vector
at all. Most set
implementations use red-black trees, which have a significant overhead. I don't know what you mean by a header adding overhead. Overhead usually refers to runtime overhead. The best use cases of set
are "I'm feeling lazy and don't want to think about it," and "I need to get this done quickly." If you care about performance, you need to profile. unordered_set
might be worth trying.
See question: How to find an item in a std::vector?
You'll also need to ensure you've implemented a suitable operator==()
for your object, if the default one isn't sufficient for a "deep" equality test.
operator==()
for my class to just be able to use std::find()
once or twice. I would only do that if it actually makes sense to add that override to the public interface of your class. Needing to be able to use std::find()
doesn't justify that. Furthermore, what if you need to do std::find()
twice but need to compare your objects in a different way? Like on a different property?
operator==
, then I would recommend using std::find_if
, then you could have re-usable predicates for your different criteria cases
Success story sharing
operator==
. If you need to account for numerical tolerance, usestd::find_if
and supply a suitable predicate.