一.find运算

假设有一个int型的vector对象,名为vec,我们想知道其中是否包含某个特定值

解决这个问题最简单的方法时使用标准库提供的find运算:

// value we'll look for int search_value = 42;  //call find to see if that value is present vector<int>::const_iterator result = find(vec.begin() , vec.end() , search_value);  //report the result cout<<"The value "<<search_value <<(result == vec.end() ? " is not present" : "is present") <<endl;

具体实现代码:

#include<iostream> #include<vector> #include<algorithm> using namespace std;  int main() { // value we'll look for    int search_value = 42;    int ival;    vector<int> vec;     while(cin>>ival)       vec.push_back(ival);        cin.clear();  //call find to see if that value is present    vector<int>::const_iterator result = find(vec.begin() , vec.end() , search_value);  //report the result   cout<<"The value "<<search_value       <<(result == vec.end() ? " is not present" : "is present")       <<endl;    return 0; }

 接下来再举一个例子:

#include <algorithm>   #include <list>   #include <iostream>      using namespace std;      int main()   {       list<int> ilist;       for (size_t i = 0; i < 10; ++i)       {           ilist.push_back(i+1);       }          ilist.push_back(10);          list<int>::iterator iLocation = find(ilist.begin(), ilist.end(), 10);   //find操作查找等于10的元素,并返回指向该元素的迭代器,如果没有找到,返回指向集合最后一个元素的迭代器        if (iLocation != ilist.end())       {           cout << "找到元素 10" << endl;       }          cout << "前一个元素为:" << *(--iLocation) << endl;          return 0;   }

类似地,由于指针的行为与作用在内置数组上的迭代器一样,因此也可以使用find来搜索数组

1 int ia[6] = {27 , 210 , 12 , 47 , 109 , 83}; 2 int search_value = 83; 3 int *result = find(ia , ia + 6 , search_value); 4 cout<<"The value "<<search_value 5     <<(result == ia + 6 ? " is not present" : "is present") 6     <<endl;

如果需要传递一个子区间,则传递指向这个子区间的第一个元素以及最后一个元素的下一位置的迭代器(或指针)。

例如,在下面对find函数的调用中,只搜索了ia[1]和ia[2]:

//only search elements ia[1] and ia[2] int *result = find(ia + 1 , ia + 3 , search_value);

 

二.find_first_of的使用

除了find之外,标准库还定义了其他一些更复杂的查找算法。当中的一部分类似string类的find操作,其中一个是find_first_of函数。

这个算法带有两对迭代器参数来标记两端元素范围:第一段范围内查找与第二段范围中任意元素匹配的元素,然后返回一个迭代器,指向第一个匹配的元素。如果找不到匹配元素,则返回第一个范围的end迭代器。

假设roster1和roster2是两个存放名字的list对象,可使用find_first_of统计有多少个名字同时出现在这两个列表中

size_t cnt = 0; list<string>::iterator it = roster1.begin();  // look in roster1 for any name also in roster2 while((it = find_first_of(it , roster1.end() , roster2.begin() , roster2.end())) != roster1.end()) {     ++cnt;     // we got a match , increment it to look in the rest of roster1     ++it; } cout<<"Found "<<cnt     <<"  names on both rosters "<<endl;

调 用find_first_of查找roster2中的每个元素是否与第一个范围内的元素匹配,也就是在it到roster1.end()范围内查找一个元 素。该函数返回此范围内第一个同时存在于第二个范围中的元素。在while的第一次循环中,遍历整个roster1范围。第二次以及后续的循环迭代则只考 虑roster1中尚未匹配的部分。

循环条件检查find_first_of的返回值,判断是否找到匹配的名字。如果找到一个匹配,则使计 数器加1,同时给it加1,使它指向roster1中的下一个元素。很明显可知,当不再有任何匹配时,find_first_of返回 roster1.end(),完成统计。

find_first_of,带有两对迭代器参数。每对迭代器中,两个参数的类型必须精确匹配,但不要求两对之间的类型匹配。特别是,元素可存储在不同类型的序列中,只要这两个序列的元素可以比较即可。

在 上述程序中,roster1和roster2的类型不必精确匹配:roster1可以使list对象,而roster2则可以使vector对象、 deque对象或者是其他后面要学到的序列。只要这两个序列的的元素可使用相等(==)操作符进行比较即可。如果roster1是list< string>对象,则roster2可以使vector<char*>对象,因为string标准库为string对象与char* 对象定义了相等(==)操作符。

 

三.find_if的使用

find_if算法 是find的一个谓词判断版本,它利用返回布尔值的谓词判断pred,检查迭代器区间[first, last)上的每一个元素,如果迭代器iter满足pred(*iter) == true,表示找到元素并返回迭代器值iter;未找到元素,则返回last。

find_if :在序列中找符合某谓词的第一个元素。

函数原型为:

1     template<class InputIterator, class Predicate>   2        InputIterator find_if(   3           InputIterator _First,    4           InputIterator _Last,    5           Predicate _Pred   6        );  

举个例子说明如下:

#include <algorithm>   #include <vector>   #include <iostream>      using namespace std;      //谓词判断函数 divbyfive : 判断x是否能5整除   bool divbyfive(int x)   {       return x % 5 ? 0 : 1;   }      int main()   {     /*       //初始vector : 方式一      //使用下标方式来操作vector     //vector随机访问方便,但插入、删除操作效率非常低     vector<int> iVect(20);        for(size_t i = 0; i < iVect.size(); ++i)   //注意:size_t     {           iVect[i] = (i+1) * (i+3);       }    */     //初始vector :方式二     vector<int> iVect;     for(vector<int>::size_type i = 0 ; i != 20 ; ++i)         iVect.push_back((i+1) * (i + 3));            //输出vector里的元素     for(vector<int>::iterator iter = iVect.begin() ; iter != iVect.end() ; ++iter)         cout<<*iter<<" ";     cout<<endl;           vector<int>::iterator iLocation;       iLocation = find_if(iVect.begin(), iVect.end(), divbyfive);          if (iLocation != iVect.end())       {           cout << "第一个能被5整除的元素为:"                << *iLocation << endl                  //打印元素:15                               << "元素的索引位置为:"                << iLocation - iVect.begin() << endl;  //打印索引位置:2       }             return 0;   }

 

四. adjacent_find算法

adjacent_find算法用于查找相等或满足条件的邻近元素对。其有两种函数原型:一种在迭代器区间[first , last)上查找两个连续的元素相等时,返回元素对中第一个元素的迭代器位置。另一种是使用二元谓词判断binary_pred,查找迭代器区间 [first , last)上满足binary_pred条件的邻近元素对,未找到则返回last。

原型:

<strong>template<class ForwardIterator>      ForwardIterator adjacent_find(         ForwardIterator _First,          ForwardIterator _Last         );   template<class ForwardIterator , class BinaryPredicate>      ForwardIterator adjacent_find(         ForwardIterator _First,          ForwardIterator _Last,                BinaryPredicate _Comp      );   </strong>

举例如下:

#include <algorithm>   #include <list>   #include <iostream>      using namespace std;      //判断X和y是否奇偶同性   bool parity_equal(int x, int y)   {       return (x - y) % 2 == 0 ? 1 : 0;   }      int main()   {       //初始化链表       list<int> iList;       iList.push_back(3);       iList.push_back(6);       iList.push_back(9);       iList.push_back(11);       iList.push_back(11);       iList.push_back(18);       iList.push_back(20);       iList.push_back(20);          //输出链表       list<int>::iterator iter;       for(iter = iList.begin(); iter != iList.end(); ++iter)       {           cout << *iter << "  ";       }       cout << endl;              //查找邻接相等的元素       list<int>::iterator iResult = adjacent_find(iList.begin(), iList.end());       if (iResult != iList.end())       {           cout << "链表中第一对相等的邻近元素为:" << endl;           cout << *iResult++ << endl;           cout << *iResult << endl;       }          //查找奇偶性相同的邻近元素       iResult = adjacent_find(iList.begin(), iList.end(), parity_equal);       if (iResult != iList.end())       {           cout << "链表中第一对奇偶相同的元素为:" << endl;           cout << *iResult++ << endl;           cout << *iResult << endl;       }       return 0;   }

 

总结:

find()            :  在序列中找某个值的第一个出现  find_if()         :    在序列中符合某谓词的第一个元素  find_first_if     :    在两个序列中找匹配元素  adjacent_find     :    用于查找相等或满足条件的邻近元素对





http://www.cnblogs.com/heyonggang/p/3241789.html