本文主要是驗證,容器搜索算法的使用: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
摘自 DriverMonkey的專欄