C++ Primer 学习中。

。。

简单记录下我的学习过程 (代码为主)



search          //从左往右找第一个符合条件的子区间    全部容器适用



find_end 
//从右往左找第一个符合条件的子区间    全部容器适用

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;
/*************************************************************************************
std::search 从左往右找子区间 全部容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2 ); template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2.
BinaryPredicate pred ); eg:
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
if (first2==last2) return first1; // specified in C++11 while (first1!=last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1==*it2) // or: while (pred(*it1,*it2)) for the pred version
{
++it1;
++it2;
if (it2==last2) return first1;
if (it1==last1) return last1;
}
++first1;
}
return last1;
}
**************************************************************************************/ /*************************************************************************************
std::find_end 从右往左找子区间 全部容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2 ); template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred ); eg:
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
if (first2==last2) return last1; // specified in C++11 ForwardIterator1 ret = last1; while (first1!=last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1==*it2) // or: while (pred(*it1,*it2)) for the pred version
{
++it1;
++it2;
if (it2==last2)
{
ret=first1;
break;
}
if (it1==last1) return ret;
}
++first1;
}
return ret;
}
**************************************************************************************/ bool mypredicate (int i, int j)
{
return (i==j);
}
bool myfunction (int i, int j)
{
return (i==j);
}
bool check(int elem,bool bo)//二元谓词
{
if(bo) return !(elem&1);
else return elem&1;
} int main ()
{
vector<int> myvector;
vector<int>::iterator it; // set some values: myvector: 10 20 30 40 50 60 70 80 90
for (int i=1; i<10; i++) myvector.push_back(i*10); // using default comparison:
int match1[] = {40,50,60,70}; it = search (myvector.begin(), myvector.end(), match1, match1+4); if (it!=myvector.end())
cout << "match1 found at position " << int(it-myvector.begin()) << endl;
else
cout << "match1 not found" << endl; // using predicate comparison:
int match2[] = {20,30,50};
it = search (myvector.begin(), myvector.end(), match2, match2+3, mypredicate); if (it!=myvector.end())
cout << "match2 found at position " << int(it-myvector.begin()) << endl;
else
cout << "match2 not found" << endl;
/**--------------------------------------find_end-----------------------------------------**/ int myints[] = {1,2,3,4,5,1,2,3,4,5,1};
deque<int> mydeque (myints,myints+11);
deque<int>::iterator itd; int match3[] = {1,2,3}; // using default comparison:
itd = find_end (mydeque.begin(), mydeque.end(), match3, match3+3); if (itd!=mydeque.end())
cout << "match1 last found at position " << int(itd-mydeque.begin()) << endl; int match4[] = {4,5,1}; // using predicate comparison:
itd = find_end (mydeque.begin(), mydeque.end(), match4, match4+3, myfunction); if (itd!=mydeque.end())
cout << "match2 last found at position " << int(itd-mydeque.begin()) << endl; /**--------------------------拓展找:偶数奇数奇数------------------------------**/ cout<<"\n1 2 3 4 5 1 2 3 4 5 1"<<endl;
vector<int> vec(myints,myints+11);
bool checkEven[3]={true,false,false};
//search
it=search(vec.begin(),vec.end(),checkEven,checkEven+3,check);
//find_end
itd=find_end(mydeque.begin(),mydeque.end(),checkEven,checkEven+3,check); if (it!=vec.end())
cout << " even odd odd found at position " << int(it-vec.begin()) << endl;
else
cout << "not found" << endl; if (itd!=mydeque.end())
cout << " even odd odd found at position " << int(itd-mydeque.begin()) << endl;
else
cout << "not found" << endl;
return 0;
} /******
Output:
match1 found at position 3
match2 not found
match1 last found at position 5
match2 last found at position 3 1 2 3 4 5 1 2 3 4 5 1
even odd odd found at position 3
even odd odd found at position 8
******/

STL_算法_查找算法(search、find_end)的更多相关文章

  1. cb30a_c++_STL_算法_查找算法_(3)search_find_end

    cb30a_c++_STL_算法_查找算法_(3)search_find_endsearch()pos = search(ideq.begin(), ideq.end(), ilist.begin() ...

  2. cb28a_c++_STL_算法_查找算法_(1)find_find_if

    cb28a_c++_STL_算法_查找算法_(1)find_find_iffind() //线性查找,比较慢.pos1 = find(ilist.begin(), ilist.end(), 5);fi ...

  3. cb33a_c++_STL_算法_查找算法_(6)binary_search_includes

    cb33a_c++_STL_算法_查找算法_(6)binary_search_includes//针对已序区间的查找算法,如set,multiset关联容器-自动排序binary_search(b,e ...

  4. cb31a_c++_STL_算法_查找算法_(4)find_first_of

    cb31a_c++_STL_算法_查找算法_(4)find_first_offind_first_of(b,e,sb,se),sb,second begin, se,second end();find ...

  5. cb34a_c++_STL_算法_查找算法_(7)_lower_bound

    cb34a_c++_STL_算法_查找算法_(7)_lower_bound//针对已序区间的查找算法,如set,multiset关联容器-自动排序lower_bound()--第一个可能的位置uppe ...

  6. cb32a_c++_STL_算法_查找算法_(5)adjacent_find

    cb32a_c++_STL_算法_查找算法_(5)adjacent_findadjacent_find(b,e),b,begin(),e,end()adjacent_find(b,e,p),p-par ...

  7. cb29a_c++_STL_算法_查找算法_(2)search_n

    cb29a_c++_STL_算法_查找算法_(2)search_n//比如:连续查找连续的n个8search_n(b,e,c,v),迭代器b,begin(),e,end().连续的c个vpos=sea ...

  8. STL_算法_查找算法(lower_bound、upper_bound、equal_range)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n)))    已序区间查找算法 lower_bound()        //找第一个符合的 ...

  9. STL_算法_查找算法(find、find_if)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...

  10. STL_算法_查找算法(binary_search、includes)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) 全部容器适用(O(log(n)))     已序区间查找算法 binary_search             //二分查 ...

随机推荐

  1. 钓鱼WIFI的防范

    实际上,Wi-Fi接入点(AP).路由器和热点常常是高度暴露的攻击面.用户一不小心就有可能踏进攻击者设置的Wi-Fi陷阱,为企业造成信息泄露或经济损失. 如今Wi-Fi 6时代悄然到来,为高密海量无线 ...

  2. top---实时动态地查看系统的整体运行情况

    top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具.通过top命令所提供的互动式界面,用热键可以管理. 语法 top(选项) 选项 -b:以批处理模式 ...

  3. [洛谷P3121] 审查(黄金) (AC自动机)

    题目描述 FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t_1...t_N.他希望从S中删除这些单词. FJ每次在S中找 ...

  4. photoshop快捷键汇总

    图层应用相关快捷键: 复制图层:Ctrl+j 盖印图层:Ctrl+Alt+Shift+E 向下合并图层:Ctrl+E 合并可见图层:Ctrl+Shift+E 激活上一图层:Alt+中括号(]) 激活下 ...

  5. COGS——T2084. Asm.Def的基本算法

    http://cogs.pro/cogs/problem/problem.php?pid=2084 ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间 ...

  6. Android异步载入全解析之开篇瞎扯淡

    Android异步载入 概述 Android异步载入在Android中使用的很广泛,除了是由于避免在主线程中做网络操作.更是为了避免在显示时由于时间太长而造成ANR,添加显示的流畅性,特别是像List ...

  7. Docker Network Configuration 高级网络配置

    Network Configuration TL;DR When Docker starts, it creates a virtual interface named docker0 on the ...

  8. 【Henu ACM Round#14 B】Duff in Love

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 让你在n的因子里面找一个最大的数字x 且x的因子全都不是完全平方数(y^2,y>1) O(sqrt(n))找出n的所有因子. ...

  9. 【Codeforces Round #424 (Div. 2) B】Keyboard Layouts

    [Link]:http://codeforces.com/contest/831/problem/B [Description] 两个键盘的字母的位置不一样; 数字键的位置一样; 告诉你第一个键盘按某 ...

  10. BZOJ——1012: [JSOI2008]最大数maxnumber || 洛谷—— P1198 [JSOI2008]最大数

    http://www.lydsy.com/JudgeOnline/problem.php?id=1012|| https://www.luogu.org/problem/show?pid=1198 T ...