upper_bound()和lower_bound()
查找非键值时返回结果
问题:当键是整数但是所需要查找的键是小数时,二则返回的位置是否相同?
结果:upper_bound采用向上取整,lower_bound采用向下取整,返回二者的位置。
测试过程
代码:
std::map<int, std::string> mMap;
mMap.insert(std::make_pair(1, "a"));
mMap.insert(std::make_pair(2, "b"));
mMap.insert(std::make_pair(3, "c"));
mMap.insert(std::make_pair(4, "d"));
mMap.insert(std::make_pair(5, "e"));
mMap.insert(std::make_pair(6, "f"));
mMap.insert(std::make_pair(7, "g"));
std::map<int, std::string>::iterator itUpper, itLower, it;
for (it = mMap.begin(); it != mMap.end(); it++)
{
std::cout << "it: ";
std::cout << (*it).first << ": "
<< (*it).second << std::endl;
}
std::cout << "==================================" << std::endl;
itLower = mMap.lower_bound(3.9);
itUpper = mMap.upper_bound(3.9);
std::cout << "itUpper: ";
std::cout << (*itUpper).first << ": "
<< (*itUpper).second << std::endl;
std::cout << "===============" << std::endl;
for (it = itUpper; it != mMap.end(); it++)
{
std::cout << "it: ";
std::cout << (*it).first << ": "
<< (*it).second << std::endl;
}
std::cout << "========================================" << std::endl;
std::cout << "itLower: ";
std::cout << (*itLower).first << ": "
<< (*itLower).second << std::endl;
std::cout << "===============" << std::endl;
for (it = itLower; it != mMap.end(); it++)
{
std::cout << "it: ";
std::cout << (*it).first << ": "
<< (*it).second << std::endl;
}
结果:
it: 1: a
it: 2: b
it: 3: c
it: 4: d
it: 5: e
it: 6: f
it: 7: g
==================================
itUpper: 4: d
===============
it: 4: d
it: 5: e
it: 6: f
it: 7: g
========================================
itLower: 3: c
===============
it: 3: c
it: 4: d
it: 5: e
it: 6: f
it: 7: g
------------------------------------分割线-----------------------------------------------
查找键值时返回结果
结果:二者返回值均为std::map<type, type>::iterator类型,但二者的返回的位置不同,对于upper_bound(key)返回的是输入key的后一个key位置,lower_bound(key)返回的是输入key的位置。
测试过程:
1、创建一个std::map<char, int>的map,并插入值。
std::map<char, int> mMap;
mMap.insert(std::make_pair('a', 10));
mMap.insert(std::make_pair('b', 20));
mMap.insert(std::make_pair('c', 30));
mMap.insert(std::make_pair('d', 40));
mMap.insert(std::make_pair('e', 50));
mMap.insert(std::make_pair('f', 60));
mMap.insert(std::make_pair('g', 70));
mMap.insert(std::make_pair('h', 80));
mMap.insert(std::make_pair('i', 90));
2、创建三个std::map<char, int>::iterator, 分别为 itUpper,itLower, it。
std::map<char, int>::iterator itUpper, itLower, it;
3、遍历mMap并输出
for(it = mMap.begin(); it != mMap.end(); it++)
{
std::cout << "it: ";
std::cout << (*it).first << ": "
<< (*it).second << std::endl;
}
输出结果:
it: a: 10
it: b: 20
it: c: 30
it: d: 40
it: e: 50
it: f: 60
it: g: 70
it: h: 80
it: i: 90
4、分别使用upper_bound()和lower_bound(),查找’c’,并输出其结果。
std::cout << "========================================" << std::endl;
itUpper = mMap.upper_bound('c');
itLower = mMap.lower_bound('c');
std::cout << "itUpper: ";
std::cout << (*itUpper).first << ": "
<< (*itUpper).second << std::endl;
std::cout << "itLower: ";
std::cout << (*itLower).first << ": "
<< (*itLower).second << std::endl;
输出结果:
========================================
itUpper: d: 40
itLower: c: 30
5、分别以itUpper和itLower为初始位置进行遍历mMap
std::cout << "========================================" << std::endl;
for(it = itUpper; it != mMap.end(); it++)
{
std::cout << "it: ";
std::cout << (*it).first << ": "
<< (*it).second << std::endl;
}
std::cout << "========================================" << std::endl;
for (it = itLower; it != mMap.end(); it++)
{
std::cout << "it: ";
std::cout << (*it).first << ": "
<< (*it).second << std::endl;
}
输出结果:
========================================
it: d: 40
it: e: 50
it: f: 60
it: g: 70
it: h: 80
it: i: 90
========================================
it: c: 30
it: d: 40
it: e: 50
it: f: 60
it: g: 70
it: h: 80
it: i: 90
6、测试输入key值不存在于mMap
std::cout << "========================================" << std::endl;
itUpper = mMap.upper_bound('w');
itLower = mMap.lower_bound('w');
if (itUpper == mMap.end())
{
std::cout << "itUpper == mMap.end()" << std::endl;
}
if (itLower == mMap.end())
{
std::cout << "itLower == mMap.end()" << std::endl;
}
输出结果:
========================================
itUpper == mMap.end()
itLower == mMap.end()
测试结果
可以得出upper_bound()返回的为输入key值的下一个key值的位置,lower_bound()返回的是输入key值的位置。若输入的key值不存在,则返回end()。
附完整代码:
#include<map>
int main()
{
std::map<char, int> mMap;
mMap.insert(std::make_pair('a', 10));
mMap.insert(std::make_pair('b', 20));
mMap.insert(std::make_pair('c', 30));
mMap.insert(std::make_pair('d', 40));
mMap.insert(std::make_pair('e', 50));
mMap.insert(std::make_pair('f', 60));
mMap.insert(std::make_pair('g', 70));
mMap.insert(std::make_pair('h', 80));
mMap.insert(std::make_pair('i', 90));
std::map<char, int>::iterator itUpper, itLower, it;
for(it = mMap.begin(); it != mMap.end(); it++)
{
std::cout << "it: ";
std::cout << (*it).first << ": "
<< (*it).second << std::endl;
}
std::cout << "========================================" << std::endl;
itUpper = mMap.upper_bound('c');
itLower = mMap.lower_bound('c');
std::cout << "itUpper: ";
std::cout << (*itUpper).first << ": "
<< (*itUpper).second << std::endl;
std::cout << "itLower: ";
std::cout << (*itLower).first << ": "
<< (*itLower).second << std::endl;
std::cout << "========================================" << std::endl;
for(it = itUpper; it != mMap.end(); it++)
{
std::cout << "it: ";
std::cout << (*it).first << ": "
<< (*it).second << std::endl;
}
std::cout << "========================================" << std::endl;
for (it = itLower; it != mMap.end(); it++)
{
std::cout << "it: ";
std::cout << (*it).first << ": "
<< (*it).second << std::endl;
}
std::cout << "========================================" << std::endl;
itUpper = mMap.upper_bound('w');
itLower = mMap.lower_bound('w');
if (itUpper == mMap.end())
{
std::cout << "itUpper == mMap.end()" << std::endl;
}
if (itLower == mMap.end())
{
std::cout << "itLower == mMap.end()" << std::endl;
}
return 1;
}
结果截图: