STL_算法_查找算法(binary_search、includes)
来源:互联网 发布:win和mac双系统 编辑:程序博客网 时间:2024/04/28 08:05
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!*/
0 0
- STL_算法_查找算法(binary_search、includes)
- C++ STL 算法:查找算法(6) binary_search、includes
- STL_算法_查找算法(search_n)
- STL_算法_查找算法(find_first_of、find_last_of)
- STL_算法_查找算法(adjacent_find)
- STL_算法_查找算法(find、find_if)
- STL_算法_查找算法(search、find_end)
- STL_算法_查找算法(lower_bound、upper_bound、equal_range)
- STL_算法(22)_ STL_算法_替换算法
- c++之STL(13) STL 算法 - 查找算法(6)binary_search(b,e,v) binary_search(b,e,v,p) includes(b,e,sb,se) include
- STL_算法(21)_ STL_算法_填充新值
- STL_算法_交换(swap_ranges)
- C++标准库---已序区间算法之一binary_search()&includes()
- STL_算法(23)_删除_(1)
- STL_算法_元素计数(count、count_if)
- STL_算法_复制元素(copy、copy_backward)
- STL_算法_删除(unique、unique_copy)
- STL_算法_逆转(reverse,reverse_copy)
- 怎么理解Condition
- 未来数据中心网络的三大武器: SDN、Overlay、VDC
- srping关于重新设置request参数问题
- 本文中,我们为大家收集了有关前端设计的9个非常有用的响应式框架。在过去的几年中响应式框架已经出现,如今它已经变得越来越普遍。因此,我们收集这些响应式框架来为您服务,希望当您在设计前端时这些框架能帮助到
- 视频相关的系列教程
- STL_算法_查找算法(binary_search、includes)
- ipython notebook http://localhost:8888/tree error
- Android Studio设置
- java流与文件——文本输入输出
- command 窗口执行.sql文件
- soap发送报文请求和dom4j解析XML并且获得指定名称的节点信息
- HDU2059龟兔赛跑
- 腾讯微信公开课分享的所有精华都在这里了(PPT都截了啊!)
- knockout整合undersocre