灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2867回复:0

[C++技术]c++学习——容器搜索方法(lower_bound, uper_bound)

楼主#
更多 发布于:2012-09-06 11:35

本文主要是验证,容器搜索算法的使用:lower_bound, uper_bound
验证项目:
1. 当 key  > begin 时 lower_bound, uper_bound 取值
2. 当  key < end   时 lower_bound, uper_boudn 取值
3. 当  key  = 容器中的某值(不等于bigin,也不等于end) 时   lower_bound, uper_boudn 取值
4. 当  key 在 不等于容器中任何一Key, 但是在key 的返回 ower_bound, uper_boudn 取值
5. 当 key 等于 bigin, 当 key 等于 end 时 的取值
测试代码:
[cpp]
#include "stdafx.h"
#include <map>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //lower_bound函数用法,这个函数用来返回要查找关键字的下界
    //upper_bound函数用法,这个函数用来返回要查找关键字的上界
    map<int,string>mp;

    mp[3]="3";
    mp[4]="4";
    mp[7]="7";
    mp[8]="8";

    map<int,string>::iterator iterLowerBound5,iterUperBound5;
    map<int,string>::iterator iterLowerBound7,iterUperBound7;
    map<int,string>::iterator iterLowerBound3,iterUperBound3;
    map<int,string>::iterator iterLowerBound8,iterUperBound8;
    map<int,string>::iterator iterLowerBound10,iterUperBound10;
    map<int,string>::iterator iterLowerBound1,iterUperBound1;

    iterLowerBound5 = mp.lower_bound(5);
    iterUperBound5  = mp.upper_bound(5);

    iterLowerBound7 = mp.lower_bound(7);
    iterUperBound7  = mp.upper_bound(7);

    iterLowerBound3 = mp.lower_bound(0);
    iterUperBound3  = mp.upper_bound(0);

    iterLowerBound8 = mp.lower_bound(8);
    iterUperBound8  = mp.upper_bound(8);
    
    iterLowerBound10 = mp.lower_bound(10);
    iterUperBound10  = mp.upper_bound(10);
    if(iterLowerBound10 == mp.end())
        cout << "iterUperBound10 = end" << endl;
    if(iterUperBound10 == mp.end())
        cout << "iterUperBound10 = end" << endl;

    iterLowerBound1 = mp.lower_bound(1);
    iterUperBound1  = mp.upper_bound(1);
    if(iterLowerBound1 == mp.end())
        cout << "iterUperBound1 = end" << endl;
    if(iterUperBound1 == mp.end())
        cout << "iterUperBound1 = end" << endl;

    if(iterLowerBound1 == mp.begin())
        cout << "iterUperBound1 = begin" << endl;
    if(iterUperBound1 == mp.begin())
        cout << "iterUperBound1 = begin" << endl;

    //iter2 = mp.upper_bound(5);

    string Str = iterLowerBound5->second;
    cout<<"lower_bound(5) = " <<Str.c_str()<<endl;
    Str = iterUperBound5->second;
    cout<<"upper_bound(5) = " <<Str.c_str()<<endl;

    Str = iterLowerBound7->second;
    cout<<"lower_bound(7) = " << Str.c_str()<<endl;
    Str = iterUperBound7->second;
    cout<<"upper_bound(7) = " << Str.c_str()<<endl;

    Str = iterLowerBound3->second;
    cout<<"lower_bound(0) = " << Str.c_str()<<endl;
    Str = iterUperBound3->second;
    cout<<"upper_bound(0) = " << Str.c_str()<<endl;

    Str = iterLowerBound8->second;
    cout<<"lower_bound(8) = " << Str.c_str()<<endl;
    //Str = iterUperBound8->second;
    if(iterUperBound8 == mp.end())
     cout<<"upper_bound(8) == end" << Str.c_str()<<endl;
      
    while(1);
    return 0;
}
打印输出:
iterLowerBound10 = end
iterUperBound10 = end
iterLowerBound1 = begin
iterUperBound1 = begin
lower_bound(5) = 7
lower_bound(5) = 7
lower_bound(7) = 7
lower_bound(7) = 8
lower_bound(0) = 3
lower_bound(0) = 3
lower_bound(8) = 8
lower_bound(8) = end8
结论:
当参数 key 没有在 容器 key的范围内:
1. 小于容器key uper_bound, lower_bound 都将返回 begin.
2. 大于容器key uper_bound, lower_bound 都将返回 end
当参数key 在容器key 范围内:
1. 参数 key == 容器key. lower_bound 将返回当前key 的iterator, uper_bound 将返回下一个元素的iterator.
2. 参数 key 不等于 容器key,且在范围内, loer_bound将返回 比参数key 大的且相邻的容器key的iterator
3 如果 Key等于 begin 或等于 end,将返回begin 或end





喜欢0 评分0
游客

返回顶部