原文地址:http://www.cplusplus.com/reference/algorithm/binary_search/
function template
<algorithm>
std::binary_search
default (1)
template <class ForwardIterator, class T>
bool binary_search (ForwardIterator first, ForwardIterator last,
const T& val);
|
custom (2)
template <class ForwardIterator, class T, class Compare>
bool binary_search (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);
|
Test if value exists in sorted sequence
Returnstrue
if any element in the range[first,last)
is equivalent toval, andfalse
otherwise.
如果在[first,end)范围内存在任一元素和val相等,则返回true,否则返回false.
例子:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void binary_s(){
vector<int> vi;
for(int i=0;i<9;i++)
vi.push_back(i);
if(binary_search(vi.begin(),vi.end(),1))
cout<<"1 exits!"<<endl;
else
cout<<"1 didn't exits!"<<endl;
if(binary_search(vi.begin(),vi.end(),7))
cout<<"7 exits!"<<endl;
else
cout<<"7 didn't exits!"<<endl;
if(binary_search(vi.begin(),vi.end(),10))
cout<<"10 exits!"<<endl;
else
cout<<"10 didn't exits!"<<endl;
}
运行截图:
The elements are compared usingoperator<
for the first version, andcompfor the second. Two elements,aandbare considered equivalent if(!(a<b) && !(b<a))
or if(!comp(a,b)
&& !comp(b,a))
.
第一个版本里面元素的比较使用<比较符。第二个个使用comp比较器,两个元素,a和b相等的条件为(!(a<b) && !(b<a)).
The elements in the range shall already besortedaccording to this same criterion (operator<
orcomp),
or at least partitionedwith respect toval.
范围内的容器应该是已经排序好了的,并且是使用相同的规则排序的(operator< or comp),至少排序到val。(即最后一个排序好的元素是val).
例子:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void binary_s2(){
vector<int> vi;
vi.push_back(1);
vi.push_back(3);
vi.push_back(4);
vi.push_back(7);
vi.push_back(2);
vi.push_back(5);
/*vi (1,3,4,7,2,5)*/
if(binary_search(vi.begin(),vi.end(),1))
cout<<"1 exits!"<<endl;
else
cout<<"1 didn't exits!"<<endl;
if(binary_search(vi.begin(),vi.end(),7))
cout<<"7 exits!"<<endl;
else
cout<<"7 didn't exits!"<<endl;
if(binary_search(vi.begin(),vi.end(),2))
cout<<"2 exits!"<<endl;
else
cout<<"2 didn't exits!"<<endl;
}
运行截图:
可以看到,7可以找到,但是2找不到!
The function optimizes the number of comparisons performed by comparing non-consecutive elements of the sorted range, which is specially efficient forrandom-access
iterators.
该方法优化了比较不连续的元素的次数,随机访问迭代器则更为高效。
The behavior of this function template is equivalent to:
该函数的行为类似下面:
1
2
3
4
5
6
|
template <class ForwardIterator, class T>
bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
{
first = std::lower_bound(first,last,val);
return (first!=last && !(val<*first));
}
|
|
Parameters
first, last
Forward iteratorsto the initial and final
positions of asorted(or properlypartitioned)
sequence. The range used is[first,last)
, which contains all the elements betweenfirstandlast, including
the element pointed byfirstbut not the element pointed bylast.
要排序序列的开头及结束位置,包括[first,last)里的所有元素,包括first,但不包括last指向的元素。
val
Value to search for in the range.
For(1),Tshall be a type supporting being compared with elements of the range[first,last)
as either operand ofoperator<
.
要查找的值。
对于(1),范围内的元素应该是已经排好序的。
comp
Binary function that accepts two arguments of the type pointed byForwardIterator(and of typeT), and returns a value convertible tobool
. The value returned
indicates whether the first argument is considered to go before the second.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
接受两个参数兵返回一个bool值的二元函数,返回的值应该是看第一个元素是否在第二个元素之前。
该函数不应该修改参数。
Return value
true
if an element equivalent tovalis found, andfalse
otherwise.
如果找到一个值和val相等,则返回true,否则返回false.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
// binary_search example
#include <iostream> // std::cout
#include <algorithm> // std::binary_search, std::sort
#include <vector> // std::vector
bool myfunction (int i,int j) { return (i<j); }
int main () {
int myints[] = {1,2,3,4,5,4,3,2,1};
std::vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1
// using default comparison:
std::sort (v.begin(), v.end());
std::cout << "looking for a 3... ";
if (std::binary_search (v.begin(), v.end(), 3))
std::cout << "found!\n"; else std::cout << "not found.\n";
// using myfunction as comp:
std::sort (v.begin(), v.end(), myfunction);
std::cout << "looking for a 6... ";
if (std::binary_search (v.begin(), v.end(), 6, myfunction))
std::cout << "found!\n"; else std::cout << "not found.\n";
return 0;
}
|
|
Output:
looking for a 3... found!
looking for a 6... not found.
|
Complexity
On average, logarithmic in thedistancebetweenfirstandlast: Performs approximatelylog2(N)+2
element
comparisons (whereNis this distance).
Onnon-random-accessiterators, the iteratoradvancesproduce
themselves an additional linear complexity inNon average.
Data races
The objects in the range[first,last)
are accessed.
Exceptions
Throws if either an element comparison or an operation on an iterator throws.
Note that invalid arguments causeundefined behavior.
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-8
于GDUT
祝大家中秋节快乐!
——————————————————————————————————————————————————————————————————
分享到:
相关推荐
algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明
用于三维光学形貌扫描完成后,生成的stl文件的读取,并形成俯视投影云图
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层.zip
C++读取STL文件,输出所有三角形的顶点坐标
STL课件_C++_课件stl_c++课件_源码.zip
STL算法(for_each/transform)
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层_源码.rar.rar
09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板...
stl的入门ppt 很好用哦,分为导入 STL的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)
STL学习教程,
STL 最新源代码
C++ STL 参考手册Cpp_STL_ReferenceManual ,对学习STL 有帮助
SGI STL是由SGI公司开发的STL库.这份STL可以用在VC上.
vc显示stl,读取顶点坐标,并显示在独立窗口中
STL切片算法,一种快速高效解决stl文件切片的程序算法讲解
在MATLAB中完成STL数据的渲染,用PATCH函数完成加颜色和亮度等功能
最新的STL源码,最新的STL源码,最新的STL源码
读取、显示STL格式的3D文件,可对二进制的STL文件进行读取、显示。显示模式可以选择有网格、无网格等。
看STL文件的小软件,可以自由的实现旋转,等功能,现在只是一小部分,以后会发后面的
十多个STL的经典例子,相当不错,初学STL必备