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. URL中一些特殊符号的替代符

    下表中列出了一些URL特殊符号及编码 十六进制值 1.+ URL 中+号表示空格 %2B 2.空格 URL中的空格可以用+号或者编码 %20 3./ 分隔目录和子目录 %2F 4.? 分隔实际的 UR ...

  2. 推荐几款常用的Eclipse插件

    Eclipse 应该说是老牌也是最常用的Java开发工具,尽管这几年 InstelliJ IDEA 的发展势头很强劲,身边使用和推崇的人也大有人在,但个人而言还是觉有些不太习惯.这里也介绍几款自己常用 ...

  3. H5新增的标签总结

    1:便于排版的Form表单 HTML5为了方便排版,可以使form的表单标签脱离form的嵌套. 方法:form指定id,所有的标签标签都添加form= id属性. 例如: <form acti ...

  4. JS实现缓存运动

                                                                                                      JS ...

  5. libvips

    libvips : an image processing library libvips is a 2D image processing library. Compared tosimilar l ...

  6. pcapy-0.10.8 安装

    (1)下载 http://corelabs.coresecurity.com/index.php?module=Wiki&action=view&type=tool&name= ...

  7. Eureka Server的REST端点

    Eureka Server的REST端点 Windows下面可以安装Curl: 使用more命令可以显示xml内容: D:\Java\IdeaProjects>more rest-api-tes ...

  8. Java类载入器

    1.   系统载入器简单介绍 Java虚拟机中能够安装多个类载入器,系统默认三个主要类载入器(BootStrap.ExtClassLoader.AppClassLoader).每一个类载入器负责载入特 ...

  9. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  10. iOS定义静态变量、静态常量、全局变量

    静态变量 当我们希望一个变量的作用域不仅仅是作用域某个类的某个对象,而是作用域整个类的时候,这时候就可以使用静态变量. staticstatic修饰的变量,是一个私有的全局变量.C或者Java中sta ...