码迷,mamicode.com
首页 > 其他好文 > 详细

子序列匹配search

时间:2019-02-13 20:46:02      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:auto   cto   相同   names   return   int   using   子序列   ace   

  版本一返回[first1,last1-(last2-first2)]中的第一个iterator i,使得满足对于[first2,last2)中的每个iterator j,*(i+(j-first2))==*j,也就是在在每个以i开头的第一个字序列中,必须与第二个子序列相同

  版本二返回[first1,last1-(last2-first2)]中的第一个iterator i,使得满足对于[first2,last2)中的每个iterator j,表达式_Comp(*(i+(j-first2)),*j)为true,如果不满足,就向后移动第一个子序列的i,直到找到第一个满足的。

template<class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 search(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2
   );
template<class ForwardIterator1, class ForwardIterator2, class Pr>
   ForwardIterator1 search(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2
      BinaryPredicate _Comp
   );

code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

template <typename Integer>
class cengruent
{
    private:
        Integer n;
    public:
        cengruent(Integer m):n(m){}
        bool operator()(Integer a,Integer b)
        {
            return (a-b)%n==0;
        }
};
int main()
{
    vector<int> v{23,46,81,2,43,19,14,98,72,51};
    vector<int> v1{1,2,3};
    auto it=search(v.begin(),v.end(),v1.begin(),v1.end(),cengruent<int>(10));
    for(auto i=it;i!=it+3;++i)
        cout<<*i<<" ";
    cout<<endl;
    return 0;
}

 

子序列匹配search

标签:auto   cto   相同   names   return   int   using   子序列   ace   

原文地址:https://www.cnblogs.com/tianzeng/p/10371660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!