64,671
社区成员
发帖
与我相关
我的任务
分享
class T
{
string id;
//......
};
std::vector<T> v;
std::vector<T>::const_iterator it = std::find_if(v.begin(), v.end(),
boost::lambda::bind(&T::id, boost::lambda::_1) == id_value);
std::vector<T*>::const_iterator it = std::find_if(v.begin(), v.end(),
(boost::lambda::_1->* &T::id) == id_value);
template <class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
要求:Unary function that accepts an element in the range as argument and returns a value convertible to bool. The value returned indicates whether the element is considered a match in the context of this function.
The function shall not modify its argument.
This can either be a function pointer or a function object.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>
struct T
{
std::string id;
};
int main( )
{
{
std::vector<T> v;
auto i = std::find_if(v.begin(),v.end(),
[](T const& t) -> bool { return t.id == "that id";}
);
}
{
std::vector<std::shared_ptr<T> > v;
auto i = std::find_if(v.begin(),v.end(),
[](std::shared_ptr<T> const& t) -> bool { return t->id == "that id";}
);
}
return 0;
}
std::vector<boost::shared_ptr<T>> v;
std::find_if(v.begin(),v.end(),boost::lambda::bind(&T::id,*boost::lambda::_1) == "test");