jmasterx jmasterx - 1 month ago 7
C++ Question

check if a std::vector contains a certain object?


Possible Duplicate:

How to find an item in a std::vector?




Is there something in algorithm.h which allows you to check if a std:: container contains something? Or a way to make one ex:

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

You You
Answer

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 */
}
Comments