lower_bound和upper_bound的用法?

wang37921 2010-05-31 04:45:53
在msdn上面查找lower_bound:
Returns an iterator to the first element in a hash_map with a key value that is equal to or greater than that of a specified key.
使用它的例子

// hash_map_lower_bound.cpp
// compile with: /EHsc
#define _DEFINE_DEPRECATED_HASH_CLASSES 0
#include <hash_map>
#include <iostream>

int main( )
{
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: const_iterator hm1_AcIter, hm1_RcIter;
typedef pair <int, int> Int_Pair;

hm1.insert ( Int_Pair ( 1, 10 ) );
//改动处
//hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 3, 30 ) );

hm1_RcIter = hm1.lower_bound( 2 );
cout << "The first element of hash_map hm1 with a key of 2 is: "
<< hm1_RcIter -> second << "." << endl;

// If no match is found for the key, end( ) is returned
hm1_RcIter = hm1. lower_bound ( 4 );

if ( hm1_RcIter == hm1.end( ) )
cout << "The hash_map hm1 doesn't have an element "
<< "with a key of 4." << endl;
else
cout << "The element of hash_map hm1 with a key of 4 is: "
<< hm1_RcIter -> second << "." << endl;

// An element at a specific location in the hash_map can be
// found using a dereferenced iterator addressing the location
hm1_AcIter = hm1.end( );
hm1_AcIter--;
hm1_RcIter = hm1. lower_bound ( hm1_AcIter -> first );
cout << "The element of hm1 with a key matching "
<< "that of the last element is: "
<< hm1_RcIter -> second << "." << endl;
}



hm1.insert ( Int_Pair ( 2, 20 ) );
这一行注释掉了~这个lower_bound不是应该返回3吗?怎么是个end啊。。。
...全文
1028 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang37921 2010-05-31
  • 打赏
  • 举报
回复
map的是不小于~
和hashmap不一样~
但是msdn上的解释的确是hashmap的。。。。
cattycat 2010-05-31
  • 打赏
  • 举报
回复
注意你用的是hash_map的lower_bound成员函数,不是stl中lower_bound算法库函数。所以你查的msdn解释的不是这个,你去看hash_map的成员函数lower_bound是怎么说明的。
chenyu2202863 2010-05-31
  • 打赏
  • 举报
回复
就是二分查找
taodm 2010-05-31
  • 打赏
  • 举报
回复
这个,楼主还是买本《stl源码剖析》吧

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧