stl find sort

网络资源修改添加。

find

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

vector<int> nVec; 

find(nVec.begin(),nVec.end(),5);

find_if
自定义结构、符合类型
假如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<class InputIterator, class Predicate> 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;
A b(“bb”,2);
A c(“ccc”,3);
A d(“dddd”,4);
a.push_back(b);
a.push_back(c);
a.push_back(d);
vector<A>::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<A>::iterator t=find_if(a.begin(),a.end(),findx("bb"));

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

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

struct compare:binary_function<A,string,bool> {
      bool operator()( A &value, string str) const
      {
          if (value.GetStr()== str)
              return true;
          else
              return false;
      }
};
//示例:
vector<A>::iterator t=find_if(a.begin(),a.end(),bind2nd(compare(),"bb"));

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

比较实用的方法:
template<typename T> bool compare_no(const T* s1 , const T* s2)
{  
     return strcmp(s1->no, s2->no) == 0;
}
template<typename T> bool less_no(const T* s1 , const T* s2)
{
      return strcmp(s1->no, s2->no) < 0;
}
template<typename T> bool compare_id(const T* s1 , const T* s2)
{
     return s1->id == s2->id;
}
template<typename T> bool less_id(const T* s1 , const T* s2)
{
     return s1->id < s2->id;
}
//排序
 std::sort(vct_device.begin(), vct_device.end(), less_id<ST_DEVICE>);
 std::sort(vct_camer.begin(), vct_camer.end(), less_no<ST_CAMERA>);
 //通过编号查找ID
 vector<ST_CAMERA*>::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<ST_CAMERA>), &tmp_cam));
 if (it_cam != vct_camer.end())
      返回值channel = (*it_cam)->channel;
//通过ID查找编号
 vector<ST_CAMERA*>::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<ST_CAMERA>), &tmp_cam));
 if (it_cam == vct_camer_secd.end())
        返回值strcpy(camera,(*it_cam)->no);

在我的工程里面用的下面一种,自定义类作为结构体,内部加友元函数。

class TCData
{
public:
    TCData()
    {
        timeagain = 0;
        times= 0;
        nMaxFail = 0;
        nMaxIter = 0;
    }
    int     times;
    int     timeagain;
    int     nMaxFail;
    int     nMaxIter;
    CString tcName;
    friend bool less_name (const TCData& a,const TCData& b)
    {
        return (a.csSpecification+a.tcName)<(b.csSpecification+b.tcName);
    }
}

//用的时候
sort(tcData.begin(),tcData.end(),less_name);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值