之前寫查找一個VECTOR中保存的一個結(jié)構(gòu)的時候,知道其中的一個數(shù)據(jù)成員,每次都是遍歷一遍,寫久了覺得好麻煩,覺得不應(yīng)該是這樣才對。果真在網(wǎng)上找到了這個方法:
用boost::bind,非常簡單:
find_if(v.begin(),v.end(),bind(&A::id,_1)==25);
如果需要,下面是完整示例代碼:
#include <algorithm>
#include <vector>
#include <boost/bind.hpp>
struct A
{
int id;
};
int main()
{
using namespace std;
using namespace boost;
vector <A> v;
find_if(v.begin(),v.end(),bind(&A::id,_1)==25);
}
//bind用法