STL_算法_查找算法(lower_bound、upper_bound、equal_range)
2016-01-13 13:19
387 查看
C++ Primer 学习中。。。
简单记录下我的学习过程 (代码为主)
//所有容器适用(O(log(n)))
已序区间查找算法
lower_bound() //找第一个符合的元素,返回位置迭代器
upper_bound() //找最后一个符合的元素,返回位置迭代器
equal_range() //找一对迭代器pair(<>,<>)
关联式容器有等效的成员函数,性能更佳
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
/*************************************************************************************
std::lower_bound 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator, class T>
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
const T& value );
template <class ForwardIterator, class T, class Compare>
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp );
//eg:
template <class ForwardIterator, class T>
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (*it<value) // or: if (comp(*it,value)), for the comp version
{ first=++it; count-=step+1; }
else count=step;
}
return first;
}
*************************************************************************************/
/*************************************************************************************
std::upper_bound 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator, class T>
ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,
const T& value );
template <class ForwardIterator, class T, class Compare>
ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp );
//eg:
template <class ForwardIterator, class T>
ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (!(value<*it)) // or: if (!comp(value,*it)), for the comp version
{ first=++it; count-=step+1; }
else count=step;
}
return first;
}
*************************************************************************************/
/*************************************************************************************
std::equal_range 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator, class T>
pair<ForwardIterator,ForwardIterator>
equal_range ( ForwardIterator first, ForwardIterator last, const T& value );
template <class ForwardIterator, class T, class Compare>
pair<ForwardIterator,ForwardIterator>
equal_range ( ForwardIterator first, ForwardIterator last, const T& value,
Compare comp );
//eg:
template <class ForwardIterator, class T>
pair<ForwardIterator,ForwardIterator>
equal_range ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it = lower_bound (first,last,value);
return make_pair ( it, upper_bound(it,last,value) );
}
*************************************************************************************/
bool mygreater (int i,int j){ return (i>j); }
int main()
{
int myints[] = {10,20,30,30,20,10,10,20};
vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
vector<int>::iterator low,up;
sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
cout<<"10 10 10 20 20 20 30 30\n";
low=lower_bound (v.begin(), v.end(), 20); // ^
up= upper_bound (v.begin(), v.end(), 20); // ^
cout << "20 lower_bound at position " << int(low- v.begin()) + 1 << endl;
cout << "20 upper_bound at position " << int(up - v.begin()) + 1 << endl;
cout << endl;
/**-------------------------------------------------------------------------------**/
pair<vector<int>::iterator,vector<int>::iterator> bounds;
// using default comparison:
// sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
bounds=equal_range (v.begin(), v.end(), 20); // ^ ^
cout<<"10 10 10 20 20 20 30 30\n";
cout << "20 bounds at positions " << int(bounds.first - v.begin());
cout << " and " << int(bounds.second - v.begin()) << endl;
// using "mygreater" as comp:
sort (v.begin(), v.end(), mygreater); // 30 30 20 20 20 10 10 10
bounds=equal_range (v.begin(), v.end(), 20, mygreater); // ^ ^
cout<<endl<<"30 30 20 20 20 10 10 10"<<endl;
cout << "20 bounds at positions " << int(bounds.first - v.begin());
cout << " and " << int(bounds.second - v.begin()) << endl;
return 0;
}
/******
Output:
10 10 10 20 20 20 30 30
20 lower_bound at position 4
20 upper_bound at position 7
10 10 10 20 20 20 30 30
bounds at positions 3 and 6
30 30 20 20 20 10 10 10
bounds at positions 2 and 5
*/
相关文章推荐
- power desinger 学习笔记<五>
- mysql 个人笔记
- LoadRunner11加载数据向导connection字符串带不过来
- 台电(Teclast)X98 Air Ⅱ 双系统版 初体验
- CSS值得关注的那些事?
- 数据库设计与技巧
- 关于XILINX SDK软件字体颜色的问题
- 仿微信中加载网页时带线行进度条的WebView的实现
- Java深拷贝与浅拷贝详解
- 2016-01-12 工作笔记
- 仿微信中加载网页时带线行进度条的WebView的实现
- 从Linux下载文件到Windows putty pscp
- HTML5 CSS3专题 纯CSS打造相冊效果
- C#控制台程序 使用 Server.MapPath,
- NSURLConnection 的使用
- BZOJ 3218|UOJ 77|A + B Problem|最大流|可持久化线段树
- 分区自动挂载与fstab文件修复-47
- 160113ios 7分割线(separator)不到左边框的问题和去掉底部多余的表格线
- Android中EditText 的setInputType以及setRawInputType区别
- Easyui使用中遇到的问题和注意事项