C++ search和search_n

  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <array>  
  4. #include <vector>  
  5. #include <functional>  
  6.   
  7. using namespace std;  
  8.   
  9. int main(){  
  10.       
  11.     array<int,8> test = {3,5,7,7,11,6,17,19};  
  12.     array<int,2> t2 = {11,13};  
  13.   
  14.     array<int,8>::iterator it;  
  15.   
  16.     //找到t2中元素在test中第一次出现的位置,不像find函数只能找一个  
  17.     it=search(test.begin(),test.end(),t2.begin(),t2.end());  
  18.   
  19.     //寻找首次连续出现2次7的位置  
  20.     it=search_n(test.begin(),test.end(),2,7);  
  21.   
  22.     //寻找首次连续出现2次大于7的位置  
  23.     it=search_n(test.begin(),test.end(),2,7,[](int i,int j){return i>j;});  
  24.   
  25.     if(it!=test.end())cout<<it-test.begin();  
  26.       
  27.   
  28.     return 0;  
  29. }  
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值