STL算法库-非修改序列式操作(二)

六、搜寻算法
6.1 第一组搜索算法函数(搜寻第一个匹配元素的位置)
原型:iterator find(iterator begin,iterator end,const T& value)
定义:找出第一个和value相等的元素的迭代器,若搜索失败,返回end

原型:iterator find_if(iterator begin,iterator end,UnaryPredicate op)
定义:找出第一个使op返回为真的元素的迭代器,若搜索失败返回end

举例如下:

#include "stdafx.h"
#include "algorithm"
#include "iostream"
#include "vector"
using namespace std;
void print(const int& i)
{
 cout<<i<<" ";
}
int _tmain(int argc, _TCHAR* argv[])
{
 vector<int> vec;
 vec.push_back(1);
 vec.push_back(2);
 vec.push_back(3);
 vec.push_back(2);

 for_each(vec.begin(),vec.end(),print);
 cout<<endl;
 vector<int>::iterator it=find(vec.begin(),vec.end(),2);
 cout<<"容Y器?中D元a素?为a2的?元a素?是?"<<*it<<endl;
 it=find_if(vec.begin(),vec.end(),bind2nd(greater<int>(),3));//值?大?于?的?元a素?
 if(it==vec.end())
  cout<<"没?找?到?大?于?的?元a素?"<<endl;
 system("pause");
 return 0;
}

6.2 第二组搜索算法函数(搜寻第一组连续n个匹配元素的位置)
原型:iterator search_n(iterator begin,iterator end,size n, const T& value)
定义:搜索第一组连续n个元素的值等于value的起始位置


原型:iterator serach_n(iterator begin,iterator end,size n,const T& value,UnaryPredicate op)
定义:搜索第一组连续n个元素使得op的值为真的元素的起始位置


举例如下:

#include "stdafx.h"
#include "algorithm"
#include "iostream"
#include "vector"
using namespace std;
void print(const int& i)
{
 cout<<i<<" ";
}
int _tmain(int argc, _TCHAR* argv[])
{
 vector<int> vec;
 vec.push_back(1);
 vec.push_back(2);
 vec.push_back(2);
 vec.push_back(2);
 vec.push_back(3);
 vec.push_back(5);
 vec.push_back(4);
 vec.push_back(2);

 for_each(vec.begin(),vec.end(),print);
 cout<<endl;
 vector<int>::iterator it=search_n(vec.begin(),vec.end(),3,2);
 cout<<"容Y器?中D连?续?3个?元a素?为a2的?起e始?位?置?是?"<<distance(vec.begin(),it)+1<<endl;
 it=search_n(vec.begin(),vec.end(),2,3,greater<int>());
 cout<<"容Y器?中D连?续?2个?元a素?大?于?的?起e始?位?置?是?"<<distance(vec.begin(),it)+1<<endl;
 system("pause");
 return 0;
}

6.3 第三组搜索算法函数(搜寻第一个子区间)
原型:iterator search(iterator1 begin,iterator1 end,iterator2 searchbegin,iterator2 searchend)
定义:搜索和区间[searchbegin,serchend]相吻合的第一个区间的位置


原型:iterator search(iterator1 begin,iterator1 end,iterator2 searchbegin,iterator2 serachend,BinaryPredicate op)
定义:op为真时才被执行,[searchbegin,searchend]为bool值元素的一个区间,搜索使op为真时的第一个区间的位置


举例如下:

#include "stdafx.h"
#include "algorithm"
#include "iostream"
#include "vector"
using namespace std;
void print(const int& i)
{
 cout<<i<<" ";
}
bool IsRight(const int& i,bool even)
{
 if(even)
  return i%2==0;
 else
  return i%2==1;
}
int _tmain(int argc, _TCHAR* argv[])
{
 vector<int> vec;
 vec.push_back(1);
 vec.push_back(2);
 vec.push_back(2);
 vec.push_back(2);
 vec.push_back(3);
 vec.push_back(5);
 vec.push_back(4);
 vec.push_back(2);


 for_each(vec.begin(),vec.end(),print);
 cout<<endl;
 int a[3]={2,2,2};
 vector<int>::iterator it=search(vec.begin(),vec.end(),a,a+2);
 cout<<"符?合?区?间?段?{2,2,2}的?区?间?的?起e始?位?置?是?"<<distance(vec.begin(),it)+1<<endl;
 bool b[3]={true,false,true};
 it=search(vec.begin(),vec.end(),b,b+2,IsRight);
 cout<<"符?合?“?偶?、?奇?、?奇?”?的?数y字?顺3序?的?区?间?的?起e始?位?置?是?"<<distance(vec.begin(),it)+1<<endl;
 system("pause");
 return 0;
}

6.4 第四组搜索函数(搜寻最后一个子区间)
原型:iterator find_end(iterator begin,iterator end,iterator2 searchbegin,iterator2 serachend)
定义:搜索符合[searhbegin,searchend]的子区间的最后一组区间的位置

原型:iterator find_end(iterator begin,iterator end,iterator2 searchbegin,iterator2 searchend,BinaryOredicate op)
定义:使op为真有效,[searchbegin,serchend]是一组包含布尔值元素的区间,op的第一个参数为[begin,end]元素,第二个参数为[serachbegin,serachend]的元素

举例如下:

#include "stdafx.h"
#include "algorithm"
#include "iostream"
#include "vector"
using namespace std;
void print(const int& i)
{
 cout<<i<<" ";
}
bool IsRight(const int& i,bool even)
{
 if(even)
  return i%2==0;
 else
  return i%2==1;
}
int _tmain(int argc, _TCHAR* argv[])
{
 vector<int> vec;
 vec.push_back(1);
 vec.push_back(2);
 vec.push_back(2);
 vec.push_back(2);
 vec.push_back(3);
 vec.push_back(5);
 vec.push_back(4);
 vec.push_back(2);

 for_each(vec.begin(),vec.end(),print);
 cout<<endl;
 int a[3]={5,4};
 vector<int>::iterator it=find_end(vec.begin(),vec.end(),a,a+2);
 cout<<"符?合?区?间?段?{5,4}的?最?后?一?个?区?间?的?起e始?位?置?是?"<<distance(vec.begin(),it)+1<<endl;
 bool b[3]={false,true};
 it=find_end(vec.begin(),vec.end(),b,b+1,IsRight);
 cout<<"符?合?“?奇?、?偶?”?的?数y字?顺3序?的?最?后?一?个?区?间?的?起e始?位?置?是?"<<distance(vec.begin(),it)+1<<endl;
 system("pause");
 return 0;
}


6.5 第五组搜索函数(搜索某些元素的第一次出现位置)
原型:iterator find_first_of(iterator begin,iterator end,iterator2 searchbegin,iterator serachend)
定义:在区间[begin,end]中逐个搜索[searchbegin,searcchend]的元素,返回找到的第一个元素的位置

原型:iterator find_first_of(iterator begin,iterator end,iterator2 searchbegin,iterator2 searchend,BinaryPredicate op)
定义:在区间[begin,end]中逐个搜索[searchbegin,searcchend]的元素,返回找到的第一个使op为真的元素的位置

举例如下:

#include "stdafx.h"
#include "algorithm"
#include "iostream"
#include "vector"
using namespace std;
void print(const int& i)
{
 cout<<i<<" ";
}
bool IsRight(const int& i,const int& j)
{
 if(i%j==0)
  return true;
 return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
 vector<int> vec;
 vec.push_back(1);
 vec.push_back(2);
 vec.push_back(2);
 vec.push_back(2);
 vec.push_back(3);
 vec.push_back(5);
 vec.push_back(4);
 vec.push_back(2);

 for_each(vec.begin(),vec.end(),print);
 cout<<endl;
 int a[3]={3,4};
 vector<int>::iterator it=find_first_of(vec.begin(),vec.end(),a,a+2);
 cout<<"符?合?区?间?段?{2,3}的?第?一?个?区?间?的?起e始?位?置?是?"<<distance(vec.begin(),it)+1<<endl;
 int b[2]={1,2};
 it=find_first_of(vec.begin(),vec.end(),b,b+1,IsRight);
 cout<<"符?合?{1,2}满?足?IsRight的?子?串?的?位?置?是?"<<distance(vec.begin(),it)+1<<endl;
 system("pause");
 return 0;
}

6.6 第六组搜索函数(搜寻两个连续相等的元素)
原型:iterator adjacent_find(iterator begin,iterator end)
定义:搜寻[begin,end]中连续相等的两个元素


原型:iterator adjacent_find(iterator begin,iterator end,Pred op)
定义:搜寻[begin,end]中连续使op为真的连续的两个元素


举例如下:

#include "stdafx.h"
#include "algorithm"
#include "iostream"
#include "vector"
using namespace std;
void print(const int& i)
{
 cout<<i<<" ";
}
bool IsRight(const int& i,const int& j)
{
 if(i%j==0)
  return true;
 return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
 vector<int> vec;
 vec.push_back(1);
 vec.push_back(2);
 vec.push_back(2);
 vec.push_back(2);
 vec.push_back(3);
 vec.push_back(5);
 vec.push_back(4);
 vec.push_back(2);

 for_each(vec.begin(),vec.end(),print);
 cout<<endl;
 int a[3]={3,4};
 vector<int>::iterator it=adjacent_find(vec.begin(),vec.end());
 cout<<"容Y器?中D连?续?相?等?的?两?个?元a素?的?起e始?位?置?是?"<<distance(vec.begin(),it)+1<<endl;
 int b[2]={1,2};
 it=adjacent_find(vec.begin(),vec.end(),IsRight);
 cout<<"满?足?第?一?个?数y为a第?二t个?数y倍?数y的?起e始?位?置?是?"<<distance(vec.begin(),it)+1<<endl;
 system("pause");
 return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值