c++之STL(13) STL 算法 - 查找算法(7)lower_bound() upper_bound() equal_range(0
来源:互联网 发布:知乎 dota2 编辑:程序博客网 时间:2024/06/06 05:06
以下三个算法对所有容器都是可以使用的(一定要先排序)
lower_bound() // 查找第一个可能的位置
upper_bound() // 查找最后一个可能的位置
equal_range() // 同时可以查找上面的两个
但是 关联式容器(主要是multiset,multimap)有等效的成员函数,性能更佳
STL 算法-查找算法
find()
find_if()
search_n()
search()
find_end()
find_first_of()
adjacent_find()
已序空间查找算法
binary_search()
includes()
lower_bound()
upper_bound()
#include<iostream>#include<algorithm>#include<list>using namespace std;int main(){list<int> ilist;for (int i = 1; i <= 9; i++)ilist.insert(ilist.end(), i);for (int i = 1; i <= 9; i++)ilist.insert(ilist.end(), i);ilist.push_back(5);ilist.push_back(5);ilist.push_back(5);ilist.sort();for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++){cout << *iter << " ";}cout << endl;list<int>::iterator pos1, pos2;pos1 = lower_bound(ilist.begin(), ilist.end(), 5);cout << "第一个5的位置:" << distance(ilist.begin(), pos1) + 1 << endl;pos2 = upper_bound(ilist.begin(), ilist.end(), 5); // >5的最后一个5的位置下一个位置cout << "大于5的第一个位置:" << distance(ilist.begin(), pos2) + 1 << endl;ilist.insert(lower_bound(ilist.begin(), ilist.end(), 5), 5);// 在第一个5的位置之前插入一个5ilist.insert(upper_bound(ilist.begin(), ilist.end(), 5), 5);// 在最后一个5的位置之后插入一个5// lower_bound() 和 upper_bound()经常一起使用 也就是equal_range for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++){cout << *iter << " ";}cout << endl;//pair<list<int>::iterator, list<int>::iterator> range;range = equal_range(ilist.begin(), ilist.end(), 5);cout << distance(ilist.begin(), range.first) + 1 << endl;cout << distance(ilist.begin(), range.second) + 1 << endl;//system("pause");return 0;}
#include<iostream>#include<algorithm>#include<list>#include<set>using namespace std;int main(){list<int> ilist;//set<int> iset;multiset<int> imset;// 插入数据imset.lower_bound(5);imset.upper_bound(5);imset.equal_range(5);// 返回pair 一对儿迭代器for (int i = 1; i <= 9; i++)ilist.insert(ilist.end(), i);for (int i = 1; i <= 9; i++)ilist.insert(ilist.end(), i);ilist.push_back(5);ilist.push_back(5);ilist.push_back(5);ilist.sort();for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++){cout << *iter << " ";}cout << endl;list<int>::iterator pos1, pos2;pos1 = lower_bound(ilist.begin(), ilist.end(), 5);cout << "第一个5的位置:" << distance(ilist.begin(), pos1) + 1 << endl;pos2 = upper_bound(ilist.begin(), ilist.end(), 5); // >5的最后一个5的位置下一个位置cout << "大于5的第一个位置:" << distance(ilist.begin(), pos2) + 1 << endl;ilist.insert(lower_bound(ilist.begin(), ilist.end(), 5), 5);// 在第一个5的位置之前插入一个5ilist.insert(upper_bound(ilist.begin(), ilist.end(), 5), 5);// 在最后一个5的位置之后插入一个5// lower_bound() 和 upper_bound()经常一起使用 也就是equal_range for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++){cout << *iter << " ";}cout << endl;//pair<list<int>::iterator, list<int>::iterator> range;range = equal_range(ilist.begin(), ilist.end(), 5);cout << distance(ilist.begin(), range.first) + 1 << endl;cout << distance(ilist.begin(), range.second) + 1 << endl;//system("pause");return 0;}
0 0
- c++之STL(13) STL 算法 - 查找算法(7)lower_bound() upper_bound() equal_range(0
- C++ STL 算法:查找算法(7) lower_bound、upper_bound、equal_range
- C++STL算法lower_bound、upper_bound、equal_range
- STL中的二分查找算法(binary_search lower_bound upper_bound equal_range)
- STL中提供-二分查找算法(binary_search lower_bound upper_bound equal_range)
- STL中提供-二分查找算法(binary_search lower_bound upper_bound equal_range)
- STL中提供-二分查找算法(binary_search lower_bound upper_bound equal_range)
- STL中二分查找相关算法(binary_search, lower_bound, upper_bound, equal_range)
- STL中提供-二分查找算法(binary_search lower_bound upper_bound equal_range)
- STL中提供-二分查找算法(binary_search lower_bound upper_bound equal_range)
- STL中的四个二分查找算法(binary_search lower_bound upper_bound equal_range)
- lower_bound和upper_bound(STL算法)
- STL----lower_bound和upper_bound算法
- STL经典算法集锦<五>之查找(lower_bound/upper_bound/binary_search)
- STL_算法_查找算法(lower_bound、upper_bound、equal_range)
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- 排序算法之快速排序的思想以及Java实现
- RecylerView使用详解(分享整理)
- ubuntu16.04 实现SVN与WEB同步解决方案
- 新浪微博加号按钮动画实现方案
- android ViewPager 简单多线程加载本地图片
- c++之STL(13) STL 算法 - 查找算法(7)lower_bound() upper_bound() equal_range(0
- flex 对datagrid列表添加图片文本 itemRender
- 【学习笔记】比较符与equals
- 【NOIP2011模拟9.7】射命丸文
- HDU5835 Danganronpa(简单推理)
- sqserver数据库导入到mysql
- STM8 设置系统时钟
- 正则表达式速查表
- openfire stopped解决