patterncppCritical
check if a std::vector contains a certain object?
Viewed 0 times
objectcheckstdvectorcertaincontains
Problem
Is there something in `
Thanks
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
Solution
Checking if
Checking if
v contains the element x:#include
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 */
}Code Snippets
#include <algorithm>
if(std::find(v.begin(), v.end(), x) != v.end()) {
/* v contains x */
} else {
/* v does not contain x */
}if(!v.empty()){
/* v is non-empty */
} else {
/* v is empty */
}Context
Stack Overflow Q#3450860, score: 729
Revisions (0)
No revisions yet.