MasterofProject

STL_ algorithm _ search algorithm (search, find_end)

Label STLSTL_ algorithmLookup algorithmSearchFind_end
597 people read comments(0) Collection Report
Classification:

Primer C++ learning...

 

Simple record of my learning process (code based)


Search / / from left to right for the first eligible container for all sub interval


Find_end / /From right to leftFind the first sub section that meets the condition of all containers.


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
Namespace STD using;
/*************************************************************************************
Std:: search to find all the containers for sub interval algorithm from left to right
--------------------------------------------------------------------------------------
<class ForwardIterator1 class, ForwardIterator2> template
Search ForwardIterator1 (First1 ForwardIterator1, last1 ForwardIterator1,
First2 ForwardIterator2, last2 ForwardIterator2);

<class ForwardIterator1 template, ForwardIterator2 class, BinaryPredicate> class
Search ForwardIterator1 (First1 ForwardIterator1, last1 ForwardIterator1,
First2 ForwardIterator2, last2. ForwardIterator2
Pred BinaryPredicate);

Eg:
ForwardIterator1 class, ForwardIterator2> template<class
Search ForwardIterator1 (First1 ForwardIterator1, last1 ForwardIterator1,
First2 ForwardIterator2, last2 ForwardIterator2)
{
If return First1 (first2==last2); / / specified in C++11

While (First1! =last1)
{
It1 ForwardIterator1 = first1;
It2 ForwardIterator2 = first2;
While (*it1==*it2) / or: while (PRED (*it1, *it2) for the pred version)
{
++it1;
++it2;
If (it2==last2) First1 return;
If (it1==last1) last1 return;
}
++first1;
}
Last1 return;
}
**************************************************************************************/

/*************************************************************************************
Std:: find_end from right to left to find a sub range of all containers suitable for algorithm
--------------------------------------------------------------------------------------
<class ForwardIterator1 class, ForwardIterator2> template
Find_end ForwardIterator1 (First1 ForwardIterator1, last1 ForwardIterator1,
First2 ForwardIterator2, last2 ForwardIterator2);

<class ForwardIterator1 template, ForwardIterator2 class, BinaryPredicate> class
Find_end ForwardIterator1 (First1 ForwardIterator1, last1 ForwardIterator1,
First2 ForwardIterator2, last2 ForwardIterator2,
Pred BinaryPredicate);

Eg:
ForwardIterator1 class, ForwardIterator2> template<class
Find_end ForwardIterator1 (First1 ForwardIterator1, last1 ForwardIterator1,
First2 ForwardIterator2, last2 ForwardIterator2)
{
If return last1 (first2==last2); / / specified in C++11

RET ForwardIterator1 = last1;

While (First1! =last1)
{
It1 ForwardIterator1 = first1;
It2 ForwardIterator2 = first2;
While (*it1==*it2) / or: while (PRED (*it1, *it2) for the pred version)
{
++it1;
++it2;
If (it2==last2)
{
ret =点;
打破;
}
如果(真的吗= =)返回页首;
}
+ +点;
}
返回页首;
}
************************************************************************************** /

布尔mypredicate(int i,j)
{
返回(我= =);
}
布尔MyFunction(int i,j)
{
返回(我= =);
}
布尔查询(int元素,布尔博)/二元谓词
{
如果(博)返回!(单元1);
否则返回元素1;
}

int main()
{
<国际> myvector矢量;
< >::int向量的位置;

//设置一些值:10 20 30 40 myvector:50 60 70 80 90
为(int i = 1;i < 10;i++)myvector push_back(我10);


/ / / / / / / / / / / /:
int Match1 [ ] = { 40,50,60,70 };

它=搜索(myvector。begin(),myvector。(),Match1,Match1 + 4);

如果(它!= myvector。())
cout <<“Match1发现位置”<< int(它myvector。begin())<< endl;
其他的
cout <<“Match1未找到”<< endl;

/ / / / / / / / / / / /:
int火柴[ ] = { 20,30,50 };
它=搜索(myvector。begin(),myvector。(),火柴,火柴+ 3,mypredicate);

如果(它!= myvector。())
cout <<“火柴发现位置”<< int(它myvector。begin())<< endl;
其他的
cout <<“火柴没有找到”<< endl;
/ * * / -------------------------------------- find_end -----------------------------------------

国际myints [ ] = { 1,2,3,4,5,1,2,3,4,5,1 };
deque < int > mydeque(myints,myints + 11);
< >::Int deque迭代器ITD;

int match3 [] = {1,2,3};

/ / / / / / / / / / / /:
ITD = find_end(mydeque。begin(),mydeque match3 match3。(),,3);

如果(ITD!= mydeque。())
cout <<“Match1最后发现在位置“<< int(ITD mydeque。begin())<< endl;

国际比赛[ ] = { 4,5,1 };

/ / / / / / / / / / / /:
ITD = find_end(mydeque。begin(),mydeque。(),比赛,比赛+ 3,myFunction);

如果(ITD!= mydeque。())
cout <<“火柴上找到位置”<< int(ITD mydeque。begin())<< endl;

/ * * --------------------------拓展找:偶数奇数奇数------------------------------ * /

cout <<“N1 2 3 4 5 1 2 3 4 5 1”<< endl;
向量< in> VEC(myints,myints + 11);
布尔checkeven [ 3 ] = {真的,假的,假的};
/搜索
它=搜索(VEC。begin(),VEC。(),checkeven,checkeven + 3,检查);
/ / find_end
ITD = find_end(mydeque。begin(),mydeque。(),checkeven,checkeven + 3,检查);

如果(它!= VEC。())
cout <<“偶奇发现位置”<< int(它的血管内皮细胞。begin())<< endl;
其他的
cout <<“未找到”的<< endl;

如果(ITD!= mydeque。())
cout <<“偶奇发现位置”<< int(ITD mydeque。begin())<< endl;
其他的
cout <<“未找到”的<< endl;
返回0;
}

/ ******
输出:
Match1发现在位置3
火柴没有找到
最后发现在位置5 Match1
火柴最后发现在位置3
    
2 3 4 5 1 2 1 3 5 4
偶数奇奇发现位置3
偶数奇奇发现位置8
****** /


猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:91235次
    • 积分:二千七百零二
    • 等级:
    • 排名:7628名第
    • 原创:265篇
    • 转载:11篇
    • 译文:1篇
    • 评论:72条
    我的博客园欢迎去看看
    http://home.cnblogs.com/ing/
    博客专栏
    音乐天地

    文章存档
    Latest comments