stl map find(const key_type& _Keyval)源码分析及图示

stl源码片段:

	iterator find(const key_type& _Keyval)
		{	// find an element in mutable sequence that matches _Keyval
		iterator _Where = lower_bound(_Keyval);
		return (_Where == end() || this->comp(_Keyval, _Key(_Where._Mynode()))
			? end() : _Where);
		}

其中lower_bound()函数的效果:返回“键值 >= key“的第一个元素位置。
执行iterator _Where = lower_bound(_Keyval);会返回两种结果:1,返回为空的右节点;2,返回等于或者大于key的元素的位置。

参考lower_bound()的源码:
	_Nodeptr _Lbound(const key_type& _Keyval) const
		{	// find leftmost node not less than _Keyval
		_Nodeptr _Pnode = _Root();
		_Nodeptr _Wherenode = _Myhead;	// end() if search fails

		while (!_Isnil(_Pnode))
			if (this->comp(_Key(_Pnode), _Keyval))
				_Pnode = _Right(_Pnode);	// descend right subtree
			else
				{	// _Pnode not less than _Keyval, remember it
				_Wherenode = _Pnode;
				_Pnode = _Left(_Pnode);	// descend left subtree
				}

		return (_Wherenode);	// return best remembered candidate
		}

对于第一种情况的演示代码:
#include <iostream>
#include <map>

int main()
{
	std::map<int, int> col1;
	col1.insert(std::make_pair(3, 1));
	col1.insert(std::make_pair(2, 1));
	col1.insert(std::make_pair(1, 1));

	std::map<int, int>::iterator it = col1.find(4);
}

在函数_Lbound()中,this->comp(_Key(_Pnode), _Keyval),即3<4成立,_Pnode搜索其Right节点(该节点为空),返回的_Wherenode指向end()
最后_Where == end()为true,_Lbound()返回end()。如下图所示:


对于第二种情况的演示代码:
#include <iostream>
#include <map>

int main()
{
	std::map<int, int> col1;
	col1.insert(std::make_pair(3, 1));
	col1.insert(std::make_pair(2, 1));
	col1.insert(std::make_pair(1, 1));
	col1.insert(std::make_pair(5, 1));

	std::map<int, int>::iterator it = col1.find(4);
}

在函数_Lbound()中,_Pnode = _Left(_Pnode);,即5>4,节点5会搜索其Left节点(该节点为空),返回的_Wherenode指向5。
然后this->comp(_Keyval, _Key(_Where._Mynode()))因为4<5返回true,_Lbound()返回end()。如下图所示:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于STL中的`map`和`unordered_map`,它们都是关联容器,用于存储键值对。它们之间的主要区别在于底层实现和性能特征。 `map`是基于红黑树实现的有序容器。它按照键的顺序存储元素,并且支持插入、查找和删除操作的平均时间复杂度为O(log n)。因为有序性质,`map`可以提供范围查找和按照键的顺序遍历等功能。 `unordered_map`是基于哈希表实现的无序容器。它使用哈希函数将键映射到桶中,并且支持插入、查找和删除操作的平均时间复杂度为O(1)。由于无序性质,`unordered_map`不能提供范围查找和按照键的顺序遍历等功能,但在大多数情况下具有更好的性能。 下面是它们的一些常见用法示例: 使用`map`: ```cpp #include <iostream> #include <map> int main() { std::map<std::string, int> myMap; // 插入键值对 myMap["Alice"] = 25; myMap["Bob"] = 30; // 查找元素 std::cout << myMap["Alice"] << std::endl; // 输出25 // 遍历所有键值对 for (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` 使用`unordered_map`: ```cpp #include <iostream> #include <unordered_map> int main() { std::unordered_map<std::string, int> myMap; // 插入键值对 myMap["Alice"] = 25; myMap["Bob"] = 30; // 查找元素 auto iter = myMap.find("Alice"); if (iter != myMap.end()) { std::cout << iter->second << std::endl; // 输出25 } // 遍历所有键值对 for (const au

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值