Reputation: 169
I have created a vector of objects, and the objects have multiple private variables:
Int x, y, id
Now i wish to iterate through this vector, and find the object just before my x and y values. The smallest value for which x & y are greater.
I've seen the vector "find" command. Could this accept multiple arguments in it's final field... Something like
(Vector.begin, vector.end, (x > object.x && y > object.y)
I've been messing around with it and googling to find a solution but I've not seen any examples involving multiple arguments in the last field of the find command?
Upvotes: 0
Views: 260
Reputation: 118021
You can use std::find_if
with a lambda
Object foo; // object you want to compare against
std::vector<Object> objs; // objects you want to look through
auto itFound = std::find_if(begin(objs),
end(objs),
[&foo](Object const& obj)
{
return obj.x > foo.x && obj.y > foo.y;
});
Upvotes: 2
Reputation: 31488
You can call find_if
with a functor or lambda that tests all the conditions you please.
http://en.cppreference.com/w/cpp/algorithm/find
Upvotes: 1