jmasterx jmasterx - 1 year ago 140
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?


You You
Answer Source

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):

    /* v is non-empty */
} else {
    /* v is empty */
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download