之前寫查找一個VECTOR中保存的一個結構的時候,知道其中的一個數據成員,每次都是遍歷一遍,寫久了覺得好麻煩,覺得不應該是這樣才對。果真在網上找到了這個方法:
用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用法
用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用法