mysql vector查找_find_if查找vector内对象的成员 作为菜鸟一直不会用也不敢用

用stl的find方法查找一个包含简单类型的vector中的元素是很简单的,例如

vector strVec;

find(strVec.begin(),strVec.end(),”aa”);

假如vector包含一个复合类型的对象呢比如

class A {

public:

A(const std::string str,int id)

{

this->str=str; this->id=id;

}

private:

std::string str; int id;

};

这个时候一般的想法是写个函数遍历这个vector,然后进行比较查找。实际上在使用STL的时候,不建议使用循环遍历的查找方法,有几个理由(参加《effictive c++》46条): 效率:泛型算法通常比循环高效。 正确性: 写循环时比调用泛型算法更容易产生错误。 可维护性: 与相应的显式循环相比,泛型算法通常使代码更干净、更直观。

实际上通过find_if泛型算法可以很优雅的达到期望的效果。

template InputIterator find_if( InputIterator _First, InputIterator _Last, Predicate_Pred );

这里的最后一个参数可是一个一元谓词,即只带一个参数且返回值限定为bool的函数对象,例如

bool compare(A& dValue)

{

if(dValue.GetStr().compare(“bb”)==0)

return true;

else

return false;

}

示例:

vector a;

A b(“aa”,4);

A c(“bb”,6);

A d(“zz”,7);

a.push_back(b);

a.push_back(c);

a.push_back(d);

vector::iterator t=find_if(a.begin(),a.end(),compare);

以上函数限定了比较的内容,如果我们想要灵活的自定义比较条件的话要如何做呢,有2个办法,一个是自定义类 ,并重载()操作符号,例如:

class findx {

public:

findx(const string str){test=str;}

string GetTest() {return test;}

bool operator()(A& dValue) {

if(dValue.GetStr().compare(test)==0)

return true;

else

return false;

}

private:

string test;

};

比较的时候只要

vector::iterator t=find_if(a.begin(),a.end(),findx(“33″));

还有一种方法是使用仿函数和绑定器。仿函数就是类似上面的重载了操作符()的自定义类,或者用struct也可以。因为他定义了操作符“()”,所以能够像函数调用一样在对象名后加上“()”,并传入对应的参数,从而执行相应的功能。这样的类型就是函数对象,从而能作为函数参数传递给find_if。

下面再说绑定器:

STL中的绑定器有类绑定器和函数绑定器两种,类绑定器有binder1st和binder2nd,而函数绑定器是bind1st和bind2nd,他们的基本目的都是用于构造一个一元的函数对象。比如这里我们可以利用bind2nd通过绑定二元函数对象中的第二个参数的方式来实现二元谓词向一元谓词的转换。

struct compare: binary_function {

bool operator()( A &value, string str) const

{

if (value.GetStr()== str)

return true;

else

return false;

}

};

示例:

vector::iterator t=find_if(a.begin(),a.end(),bind2nd(compare(),”33″));

无论是用vector的循环还是find_if泛型算法,在性能和代码复杂度上面都有一定得权衡,至于在实际应用中,还是需要具体问题具体分析的。

现在还是迷糊的,下面是自己在项目中看到的师傅写的一个比较实用的方法:

template bool compare_no(const T* s1 , const T* s2)

{

return strcmp(s1->no, s2->no) == 0;

}

template bool less_no(const T* s1 , const T* s2)

{

return strcmp(s1->no, s2->no) < 0;

}

template bool compare_id(const T* s1 , const T* s2)

{

return s1->id == s2->id;

}

template bool less_id(const T* s1 , const T* s2)

{

return s1->id < s2->id;

}

//排序

std::sort(vct_device.begin(), vct_device.end(), less_id);

std::sort(vct_camer.begin(), vct_camer.end(), less_no);

//通过编号查找ID

vector::iterator it_cam;

ST_CAMERA tmp_cam;

strcpy(tmp_cam.no, "888888");

it_cam = std::find_if(vct_camer.begin(),vct_camer.end(),bind2nd(ptr_fun(compare_no), &tmp_cam));

if (it_cam != vct_camer.end())

返回值channel = (*it_cam)->channel;

//通过ID查找编号

vector::iterator it_cam;

ST_CAMERA tmp_cam;

int camid = 0;

tmp_cam.id = 3;

it_cam = std::find_if(vct_camer_secd.begin(), vct_camer_secd.end(), bind2nd(ptr_fun(compare_id), &tmp_cam));

if (it_cam == vct_camer_secd.end())

返回值strcpy(camera,(*it_cam)->no);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值