C++ Primer 学习中。。

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

全部容器适用(O(log(n)))     已序区间查找算法



binary_search             //二分查找。返回bool值,



includes                    //包括查找,返回bool值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
/*****************************************
//全部容器适用(O(log(n)))
已序区间查找算法
binary_search() //二分查找。返回bool值,
includes() //包括查找,返回bool值。
*****************************************/
/*************************************************************************************
std::binary_search 全部排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator, class T>
bool binary_search ( ForwardIterator first, ForwardIterator last,
const T& value ); template <class ForwardIterator, class T, class Compare>
bool binary_search ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp ); //eg:
template <class ForwardIterator, class T>
bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value )
{
first = lower_bound(first,last,value);
return (first!=last && !(value<*first));
}
*************************************************************************************/ /*************************************************************************************
std::includes 全部排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class InputIterator1, class InputIterator2>
bool includes ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 ); template <class InputIterator1, class InputIterator2, class Compare>
bool includes ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, Compare comp ); //eg:
template <class InputIterator1, class InputIterator2>
bool includes ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 )
{
while (first1!=last1)
{
if (*first2<*first1) break;
else if (*first1<*first2) ++first1;
else { ++first1; ++first2; }
if (first2==last2) return true;
}
return false;
}
*************************************************************************************/ bool myfunction (int i,int j) { return (i<j);} int main()
{
int myints[] = {1,2,3,4,5,4,3,2,1};
vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1 // using default comparison:
sort (v.begin(), v.end()); cout << "looking for a 3... ";
if (binary_search (v.begin(), v.end(), 3))
cout << "found!\n"; else cout << "not found.\n"; // using myfunction as comp:
sort (v.begin(), v.end(), myfunction); cout << "looking for a 6... ";
if (binary_search (v.begin(), v.end(), 6, myfunction))
cout << "found!\n"; else cout << "not found.\n";
cout<<endl;
/**----------------------------------------------------------------------------------**/
int container[] = {5,15,10,25,20,35,30,50,45,40};
int continent[] = {40,30,20,10}; sort (container,container+10);
sort (continent,continent+4); // using default comparison:
if ( includes(container,container+10,continent,continent+4) )
cout << "container includes continent!" << endl; // using myfunction as comp:
if ( includes(container,container+10,continent,continent+4, myfunction) )
cout << "container includes continent!" << endl; return 0;
} /*****
Output
looking for a 3... found!
looking for a 6... not found. container includes continent!
container includes continent! */

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

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

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

  2. 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 ...

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

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

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

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

  5. 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 ...

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

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

  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 ...

随机推荐

  1. vue无缝滚动的插件开发填坑分享

    写插件的初衷 1.项目经常需要无缝滚动效果,当时写jq的时候用用msClass这个老插件,相对不上很好用. 2.后来转向vue在vue-awesome没有找到好的无缝滚动插件,除了配置swiper可以 ...

  2. VUE:路由

    VUE:路由 一.说明 1)官方提供的用来实现SPA的vue插件 2)github:https://github.com/vuejs/vue-router 3)中文文档:http://router.v ...

  3. C语言 将十六进制字符串转为十六进制数 (二进制、十进制都适用)

    主要利用 long int strtol(const char *nptr,char **endptr,int base); 函数 函数说明: 参数base范围从2至36,或0.参数base代表采用的 ...

  4. 2015 Multi-University Training Contest 4 hdu 5334 Virtual Participation

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  5. spring boot约定优于配置的这种做法在如今越来越流行了

    约定优于配置的这种做法在如今越来越流行了,它的特点是简单.快速.便捷.但是这是建立在程序员熟悉这些约定的前提上.而 Spring 拥有一个庞大的生态体系,刚开始转到 Spring Boot 完全舍弃 ...

  6. QT5 OpenGL (六, 键盘事件, 开关灯,放大缩小综合运用)

    概要 实例效果图 立体图放大图 立体图缩小图 不加矢量开灯图 不加矢量关灯图 加矢量关灯图1 加矢量关灯图2 部分代码展示 主要内容解析 QT键盘事件 立体图形的放大和缩小 上下左右键以及A键D争键控 ...

  7. Android-Universal-Image-Loader学习笔记(3)--内存缓存

    前面的两篇博客写了文件缓存.如今说说Android-Universal-Image-Loader的内存缓存.该内存缓存涉及到的类如图所看到的 这些类的继承关系例如以下图所看到的: 如同文件缓存一样,内 ...

  8. Java5新特性之枚举

    1.  概念 首先,枚举并非一种新技术,而是一种基础数据类型.它隶属于两种基础类型中的值类型,例如以下: 2.  为什么要有枚举 枚举在真正的开发中是非经常常使用的,它的作用非常easy也非常纯粹:它 ...

  9. 对Shell几个冷知识的总结(IFS,数组,替换,分割,查找)

    IFS: 对IFS的用处直接进行说明,详细IFS是干什么的...自行谷歌 首先创建一个 "a a",和"a"的文件: 然后我们 ls查看一下: --> l ...

  10. CSS3个人盲点总结【总结中..........】

    ~:表示同辈元素之后指定类型的元素,如;elm1 ~ elm2表示,elm1之后的所有elm2元素,且elm1与elm2都是在同一个父级元素. +:表示同辈元素的兄弟元素. \A:一个空白换行符 &l ...