STL_算法_查找算法(binary_search、includes)
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)的更多相关文章
- cb33a_c++_STL_算法_查找算法_(6)binary_search_includes
cb33a_c++_STL_算法_查找算法_(6)binary_search_includes//针对已序区间的查找算法,如set,multiset关联容器-自动排序binary_search(b,e ...
- 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 ...
- cb28a_c++_STL_算法_查找算法_(1)find_find_if
cb28a_c++_STL_算法_查找算法_(1)find_find_iffind() //线性查找,比较慢.pos1 = find(ilist.begin(), ilist.end(), 5);fi ...
- cb34a_c++_STL_算法_查找算法_(7)_lower_bound
cb34a_c++_STL_算法_查找算法_(7)_lower_bound//针对已序区间的查找算法,如set,multiset关联容器-自动排序lower_bound()--第一个可能的位置uppe ...
- 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 ...
- cb30a_c++_STL_算法_查找算法_(3)search_find_end
cb30a_c++_STL_算法_查找算法_(3)search_find_endsearch()pos = search(ideq.begin(), ideq.end(), ilist.begin() ...
- 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 ...
- STL_算法_查找算法(lower_bound、upper_bound、equal_range)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n))) 已序区间查找算法 lower_bound() //找第一个符合的 ...
- STL_算法_查找算法(find、find_if)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...
随机推荐
- Spring配置文件中指定init-method属性的作用
bean 配置文件属性 init-method 用于在bean初始化时指定执行方法,用来替代继承 InitializingBean接口.相关链接:https://www.cnblogs.com/Joe ...
- tortoiseGit怎么记住密码
tortoiseGit每次pull和push的时候都要输入git密码很是麻烦,下面是tortoiseGit记住密码的步骤 首先在你的项目界面右键 选择setting,这个步骤知识看一下你的名称和ema ...
- shell 键盘录入和运算
一.read 命令,从键盘读入数据,赋给变量 1.脚本代码 #!/bin/sh read arg1 arg2 echo "第一个参数: $arg1" echo "第二个参 ...
- angular-基础
AngularJs特点: 1.依赖注入 2.模块化 3.双向绑定 4.语义化标签 当网页加载完毕,AngularJS 自动开启. ng-app 指令告诉 AngularJS,<div> 元 ...
- android canvas 画图笔记
android canvas 画图笔记 1.PathEffect类 画虚线 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); p.setStyle(Paint.S ...
- HDU 4331 Contest 4
一个很直观的想法是,求出每个点上下左右能到达的最大长度.然后枚举其斜边...没想到过了.... 当然,题解有一个很巧妙的优化,利用树状数组,那个太巧妙了. #include<iostream&g ...
- POJ 3088
已知n,求n中取k(k<=n)个数组成的m(m<=n)个的集合的排列数. 于是,可以枚举选出k个数及枚举m个集合.这个很明显是二类斯特林数.而集合有序,则乘上m! #include < ...
- Oracle SGA具体解释
SGA(SYSTEM Global Area )系统全局区 l 数据快速缓存 在Oracle进行数据处理的过程中,代价最昂贵的就是物理 I/O操作了.相同的数据从内存中得到要比从磁盘上读取快的多. 因 ...
- @crossorigin注解跨域
在@controller中类的头部有一个@CrossOrigin注解. @CrossOrigin是用来处理跨域请求的注解 先来说一下什么是跨域: (站在巨人的肩膀上) 跨域,指的是浏览器不能执行其他网 ...
- codevs 3372 选学霸(hash+并查集+多重背包)
先通过并查集处理出来有多少种不同的集合,每一个集合有多少人.一定要不要忘记了与别的没有联系的独立点. 并查集的时候能够通过hash处理出来每一个数目同样的集合的个数. 这样以人数为权值.个数为限制进行 ...