lower_bound, upper_bound,equal_range
2013-08-10 12:53
337 查看
template<class FwdIt, class T> FwdIt lower_bound(FwdIt first, FwdIt last, const T& val); template<class FwdIt, class T, class Pred> FwdIt lower_bound(FwdIt first, FwdIt last, const T& val, Pred pr); template<class FwdIt, class T> FwdIt upper_bound(FwdIt first, FwdIt last, const T& val); template<class FwdIt, class T, class Pred> FwdIt upper_bound(FwdIt first, FwdIt last, const T& val, Pred pr);
lower_bound:返回第一个“大于等于value”的元素的位置,这是可以插入元素为value且不破坏区间顺序的第一个位置
upper_bound:返回第一个“大于value”的元素位置,这是可以插入且不破坏区间顺序的最后一个位置
#include <iostream> #include <list> #include <algorithm> using namespace std ; int main() { list<int> coll ; for (int i = 0; i < 10; ++i) { coll.push_back(i) ; coll.push_back(i) ; } coll.sort() ; for (list<int>::iterator it = coll.begin(); it != coll.end(); ++it) { cout << *it << " " ; } cout << endl ; list<int>::iterator pos1, pos2 ; pos1 = lower_bound(coll.begin(), coll.end(), -1) ; pos2 = upper_bound(coll.begin(), coll.end(), -1) ; cout << distance(coll.begin(), pos1) + 1 << distance(coll.begin(),pos2) + 1 << " " << endl ; system("pause") ; return 0 ; }
看出11确实是第一个大于等于元素的位置,13是第一个大于元素的位置
如果把插入的元素换成-1,
都是位置1,都是第一个大于其的位置
****************************************************
equal_range:
template<class FwdIt, class T> pair<FwdIt, FwdIt> equal_range(FwdIt first, FwdIt last, const T& val); template<class FwdIt, class T, class Pred> pair<FwdIt, FwdIt> equal_range(FwdIt first, FwdIt last, const T& val, Pred pr);返回可以插入元素的区间,其返回值相当于make_pair(lower_bound(), upper_bound()) 由lower_bound(),upper_bound算法获得结果
#include <iostream> #include <list> #include <algorithm> using namespace std ; int main() { list<int> coll ; for (int i = 0; i < 10; ++i) { coll.push_back(i) ; coll.push_back(i) ; } coll.sort() ; for (list<int>::iterator it = coll.begin(); it != coll.end(); ++it) { cout << *it << " " ; } cout << endl ; pair<list<int>::iterator, list<int>::iterator> range ; range = equal_range(coll.begin(), coll.end(), 5) ; cout << distance(coll.begin(), range.first) + 1 << distance(coll.begin(),range.second) + 1 << " " << endl ; system("pause") ; return 0 ; }
这里返回一个pair类型,结果如下:
可以在第11和13位置插入。
相关文章推荐
- STL algorithm之count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- 转:正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range
- stl algorithm : count、find、lower_bound、upper_bound和equal_range的区别
- C++ STL 算法:查找算法(7) lower_bound、upper_bound、equal_range
- STL algorithm之count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- C++标准库---已序区间算法之二lower_bound()&upper_bound()&equal_range()
- c++之STL(13) STL 算法 - 查找算法(7)lower_bound() upper_bound() equal_range(0
- STL_算法_查找算法(lower_bound、upper_bound、equal_range)
- C++10.5 multimap和multiset类型---equal_range、lower_bound 和upper_bound函数
- STL algorithm之count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- upper_bound,lower_bound,eaual_bound,equal_range二分搜索
- 注意count,find,binary_seach,lower_bound,upper_bound和equal_range的区别
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)
- STL中二分查找相关算法(binary_search, lower_bound, upper_bound, equal_range)
- multimap使用lower_bound、 upper_bound、 equal_range进行特定key的遍历
- STL中查找方法用法总结-adjacent_find, binary_search, includes, lower_bound, upper_bound, equal_range
- STL_Algorithm: lower_bound, upper_bound, equal_range
- std::upper_bound & lower_bound & equal_range
- multimap使用lower_bound、 upper_bound、 equal_range进行特定key的遍历
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)