C++的STL之search_n

来源:互联网 发布:装修设计图软件 编辑:程序博客网 时间:2024/04/29 08:31

       C++STL的非变易算法(Non-mutating algorithms)是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理、元素查找、子序列搜索、统计和匹配。

      重复元素子序列搜索search_n算法:搜索序列中是否有一系列元素值均为某个给定值的子序列,它有如下两个函数原型,分别在迭代器区间[first, last)上搜索是否有count个连续元素,其值均等于value(或者满足谓词判断binary_pred的条件),返回子序列首元素的迭代器,或last以表示没有重复元素的子序列。

     函数原型:

[cpp] view plain copy
  1. template<class ForwardIterator1, class Diff2, class Type>  
  2.    ForwardIterator1 search_n(  
  3.       ForwardIterator1 _First1,   
  4.       ForwardIterator1 _Last1,  
  5.       Size2 _Count,   
  6.       const Type& _Val  
  7.    );  
  8. template<class ForwardIterator1, class Size2, class Type, class BinaryPredicate>  
  9.    ForwardIterator1 search_n(  
  10.       ForwardIterator1 _First1,   
  11.       ForwardIterator1 _Last1,  
  12.       Size2 _Count,   
  13.       const Type& _Val,  
  14.       BinaryPredicate _Comp  
  15.    );  
  16.    


    示例程序:

     搜索向量容器ivect = {1,8,8,8,4,,4,3}中有三个连续元素为8,没有四个连续元素8,以及有三个连续元素的两倍为16.

[cpp] view plain copy
  1. /******************************************************************* 
  2.  * Copyright (C) Jerry Jiang             
  3.  * File Name   : search_n.cpp 
  4.  * Author      : Jerry Jiang 
  5.  * Create Time : 2011-10-11 22:23:47 
  6.  * Mail        : jbiaojerry@gmail.com 
  7.  * Blog        : http://blog.csdn.net/jerryjbiao               
  8.  * Description :  简单的程序诠释C++ STL算法系列之十一                 
  9.  *                非变易算法 : 重复元素子序列搜索search_n       
  10.  ******************************************************************/  
  11.   
  12. #include <algorithm>  
  13. #include <vector>  
  14. #include <iostream>  
  15.   
  16. bool twice(const int para1, const int para2)  
  17. {  
  18.     return 2 * para1 == para2;  
  19. }  
  20.   
  21. using namespace std;  
  22.   
  23. int main()  
  24. {  
  25.     vector<int> ivect;  
  26.     ivect.push_back(1);  
  27.     ivect.push_back(8);  
  28.     ivect.push_back(8);  
  29.     ivect.push_back(8);  
  30.     ivect.push_back(4);  
  31.     ivect.push_back(4);  
  32.     ivect.push_back(3);  
  33.   
  34.     vector<int>::iterator iLocation;  
  35.     iLocation = search_n(ivect.begin(), ivect.end(), 3, 8);  
  36.     if (iLocation != ivect.end())  
  37.     {  
  38.         cout << "在ivect中找到3个连续的元素8" << endl;  
  39.     }  
  40.     else  
  41.     {  
  42.         cout << "在ivect中没有3个连续的元素8" << endl;  
  43.     }  
  44.   
  45.     iLocation = search_n(ivect.begin(), ivect.end(), 4, 8);  
  46.     if (iLocation != ivect.end())  
  47.     {  
  48.         cout << "在ivect中找到4个连续的元素8" << endl;  
  49.     }  
  50.     else  
  51.     {  
  52.         cout << "在ivect中没有4个连续的元素8" << endl;  
  53.     }  
  54.   
  55.     iLocation = search_n(ivect.begin(), ivect.end(), 2, 4);  
  56.     if (iLocation != ivect.end())  
  57.     {  
  58.         cout << "在ivect中找到2个连续的元素4" << endl;  
  59.     }  
  60.     else  
  61.     {  
  62.         cout << "在ivect中没有2个连续的元素4" << endl;  
  63.     }  
  64.   
  65.     iLocation = search_n(ivect.begin(), ivect.end(), 3, 16, twice);  
  66.     if (iLocation != ivect.end())  
  67.     {  
  68.         cout << "在ivect中找到3个连续元素的两倍为16" << endl;  
  69.     }  
  70.     else  
  71.     {  
  72.         cout << "在ivect中没有3个连续元素的两倍为16" << endl;  
  73.     }  
  74.     return 0;  
  75. }  
0 0
原创粉丝点击