C++ std::map中的upper_bound和lower_bound函数查询非键值时返回值的区别

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;
}

结果截图:
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值