STL之Binary search
来源:互联网 发布:淘宝企业店铺品牌授权 编辑:程序博客网 时间:2024/06/05 16:32
STL之Binary search
STL中对于有序序列(vector,list等)提供了相当相当强大的二分搜索Binary search算法。对于可以随机访问容器(如vector等),binary search负载度为对数级别(LogN),对于非随机访问容器(如list),则算法复杂度为线性。现在简要介绍一下几种常用的binary search算法:ForwardIterator lower_bound (ForwardIterator first,ForwardIterator last, const T& value)
ForwardIterator lower_bound (ForwardIterator first,ForwardIterator last, const T& value, Compare comp)
//查找最远的一个iterator i, 使得[first,i)里面的任意一个iterator j,有 *j < value or comp(*j,value)==true.
ForwardIterator upper_bound (ForwardIterator first,ForwardIterator last, const T& value)
ForwardIterator upper_bound (ForwardIterator first,ForwardIterator last, const T& value, Compare comp)
//查找最远的一个iterator i, 使得[first,i)里面的任意一个iterator j,有 !(*j < value) or comp(*j,value)==false.
pair equal_range(ForwardIterator first, ForwardIterator last, const T& value)
pair equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp)
//查找最大的subrange[i,j),使得任意一个iterator in [i,j)满足!(*k<value) && !(value<*k) or comp(*k,value)==false && comp(value,*k)==false.
bool binary_search (ForwardIterator first, ForwardIterator last, const T& value)
bool binary_search (ForwardIterator first, ForwardIterator last, const T& value, Compare comp)
// 查找是否在[first,last)中存在iterator i,满足 !(*i<value) && !(value<*i) or comp(*i,value)==false && comp(value,*i)==false
// 存在则返回true,否则返回false.
#include<algorithm>
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
vector<int>v;
vector<int>::iterator itr;
pair< vector<int>::iterator, vector<int>::iterator >vecpair;
for(int i = 1; i <= 20; i++){
v.push_back(i%6);
}
sort(v.begin(),v.end());
cout << "array: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl;
//lower_bound
cout << "lower_bound function, value = 3:" << endl;
itr = lower_bound(v.begin(),v.end(),3);
cout << " [first, itr) = ";
copy(v.begin(),itr,ostream_iterator<int>(cout," "));
cout << endl;
cout << " [itr, last) = ";
copy(itr,v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
// upper_bound
cout << "upper_bound function, value = 3:" << endl;
itr = upper_bound(v.begin(),v.end(),3);
cout << " [first, itr) = ";
copy(v.begin(),itr,ostream_iterator<int>(cout," "));
cout << endl;
cout << " [itr, last) = ";
copy(itr,v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
// equal_range
cout << "equal_range function, value = 3:" << endl;
vecpair = equal_range(v.begin(),v.end(),3);
cout << " [vecpair->first, vecpair->second) = ";
copy(vecpair.first, vecpair.second, ostream_iterator<int>(cout," "));
cout << endl << endl;
//binary_search, value = 3
cout << "binary_search function, value = 3:" << endl;
cout << "3 is " << (binary_search(v.begin(),v.end(),3) ? "":"not ") << "in array." << endl;
cout << endl;
//binary_search, value = 6
cout << "binary_search function, value = 6:" << endl;
cout << "6 is " << (binary_search(v.begin(),v.end(),6) ? "":"not ") << "in array." << endl;
cout << endl;
return 0;
}
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
vector<int>v;
vector<int>::iterator itr;
pair< vector<int>::iterator, vector<int>::iterator >vecpair;
for(int i = 1; i <= 20; i++){
v.push_back(i%6);
}
sort(v.begin(),v.end());
cout << "array: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl;
//lower_bound
cout << "lower_bound function, value = 3:" << endl;
itr = lower_bound(v.begin(),v.end(),3);
cout << " [first, itr) = ";
copy(v.begin(),itr,ostream_iterator<int>(cout," "));
cout << endl;
cout << " [itr, last) = ";
copy(itr,v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
// upper_bound
cout << "upper_bound function, value = 3:" << endl;
itr = upper_bound(v.begin(),v.end(),3);
cout << " [first, itr) = ";
copy(v.begin(),itr,ostream_iterator<int>(cout," "));
cout << endl;
cout << " [itr, last) = ";
copy(itr,v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
// equal_range
cout << "equal_range function, value = 3:" << endl;
vecpair = equal_range(v.begin(),v.end(),3);
cout << " [vecpair->first, vecpair->second) = ";
copy(vecpair.first, vecpair.second, ostream_iterator<int>(cout," "));
cout << endl << endl;
//binary_search, value = 3
cout << "binary_search function, value = 3:" << endl;
cout << "3 is " << (binary_search(v.begin(),v.end(),3) ? "":"not ") << "in array." << endl;
cout << endl;
//binary_search, value = 6
cout << "binary_search function, value = 6:" << endl;
cout << "6 is " << (binary_search(v.begin(),v.end(),6) ? "":"not ") << "in array." << endl;
cout << endl;
return 0;
}
array:
0 0 0 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 5
lower_bound function, value = 3:
[first, itr) = 0 0 0 1 1 1 1 2 2 2 2
[itr, last) = 3 3 3 4 4 4 5 5 5
upper_bound function, value = 3:
[first, itr) = 0 0 0 1 1 1 1 2 2 2 2 3 3 3
[itr, last) = 4 4 4 5 5 5
equal_range function, value = 3:
[vecpair->first, vecpair->second) = 3 3 3
binary_search function, value = 3:
3 is in array.
binary_search function, value = 6:
6 is not in array.
0 0 0 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 5
lower_bound function, value = 3:
[first, itr) = 0 0 0 1 1 1 1 2 2 2 2
[itr, last) = 3 3 3 4 4 4 5 5 5
upper_bound function, value = 3:
[first, itr) = 0 0 0 1 1 1 1 2 2 2 2 3 3 3
[itr, last) = 4 4 4 5 5 5
equal_range function, value = 3:
[vecpair->first, vecpair->second) = 3 3 3
binary_search function, value = 3:
3 is in array.
binary_search function, value = 6:
6 is not in array.
- STL之Binary search
- STL之二分查找 (Binary search in STL)
- STL之二分查找 (Binary search in STL)
- STL之二分查找 (Binary search in STL)
- STL之二分查找 (Binary search in STL)
- STL之二分查找 (Binary search in STL)
- STL之二分查找 (Binary search in STL)
- 【 STL之二分查找 (Binary search in STL)】
- binary search in STL
- Codeforces 895B (stl binary search)
- binary-search之Scheme实现
- STL源码剖析 [容器](十)[Binary search tree]
- 数据结构之折半查找法(Binary Search)
- 基本数据结构之Binary Search Tree
- LeetCode 之 Unique Binary Search Trees
- LeetCode 之 Recover Binary Search Tree
- LeetCode之Unique Binary Search Trees
- LeetCode 之二分法查找 Binary search
- 各种应用访问jboss5.0管理数据源方式
- C++ 常量指针或者常量引用作为函数参数传递的原因
- shell语法if else elif
- shell语法for
- POJ 1195 二维线段树||二维树状数组
- STL之Binary search
- 获取IOS应用安装列表
- linux下C语言中的flock函数用法
- shell语法function
- NEFU 698 Post office 大概是dp?
- couchbase安装fedora18上的问题
- javascript Table
- 基础加强—06—类加载器
- shell语法while