search,search_n

1.     search算法在一个序列中找另一个序列的第一次出现的位置。

search(A.begin(),A.end(), B.begin(), B.end());

在A中找B这个序列的第一次出现。


2.    重复元素子序列搜索search_n算法:搜索序列中是否有一系列元素值均为某个给定值的子序列


#include<iostream>
#include<list>

using namespace std;

int main(){
        list<int> list1 ={1,2,3,4,5,6};
        list<int> list2 ={3,4,5,6};

        list<int>::iterator it;
        it = search(list1.begin(),list1.end(),list2.begin(),list2.end());

        int index = 0;
        if(it!=list1.end()){
                while(it != list1.begin()){
                        it--;
                        index++;
                }
                cout<<"search...list1["<<index<<"]"<<endl;
                }
        else
                cout<<"search false!"<<endl;
}






#include <algorithm>
#include <vector>
#include <iostream>

bool twice(const int para1, const int para2)
{
return 2 * para1 == para2;
}

using namespace std;

int main()
{
vector<int> ivect;
ivect.push_back(1);
ivect.push_back(8);
ivect.push_back(8);
ivect.push_back(8);
ivect.push_back(4);
ivect.push_back(4);
ivect.push_back(3);


vector<int>::iterator iLocation;
iLocation = search_n(ivect.begin(), ivect.end(), 3, 8);
if (iLocation != ivect.end())
{
cout << "在ivect中找到3个连续的元素8" << endl;
}
else
{
cout << "在ivect中没有3个连续的元素8" << endl;
}


iLocation = search_n(ivect.begin(), ivect.end(), 4, 8);
if (iLocation != ivect.end())
{
cout << "在ivect中找到4个连续的元素8" << endl;
}
else
{
cout << "在ivect中没有4个连续的元素8" << endl;
}


iLocation = search_n(ivect.begin(), ivect.end(), 2, 4);
if (iLocation != ivect.end())
{
cout << "在ivect中找到2个连续的元素4" << endl;
}
else
{
cout << "在ivect中没有2个连续的元素4" << endl;
}


iLocation = search_n(ivect.begin(), ivect.end(), 3, 16, twice);
if (iLocation != ivect.end())
{
cout << "在ivect中找到3个连续元素的两倍为16" << endl;
}
else
{
cout << "在ivect中没有3个连续元素的两倍为16" << endl;
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值