标签:
STL内部实现的rbtree,实现 lower_bound/upper_bound 过程,是从 begin() 开始向 end() 进行遍历,将元素的 key 与目标 key 进行比较,直至找到的第一个符合要求的 iterator 为止!具体看代码,如下
位于bits/stl_tree.h
1 template<typename _Key, typename _Val, typename _KeyOfValue, 2 typename _Compare, typename _Alloc> 3 typename _Rb_tree<_Key, _Val, _KeyOfValue, 4 _Compare, _Alloc>::const_iterator 5 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>:: 6 _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y, 7 const _Key& __k) const 8 { 9 while (__x != 0) 10 if (!_M_impl._M_key_compare(_S_key(__x), __k)) 11 __y = __x, __x = _S_left(__x); 12 else 13 __x = _S_right(__x); 14 return const_iterator(__y); 15 } 16 17 template<typename _Key, typename _Val, typename _KeyOfValue, 18 typename _Compare, typename _Alloc> 19 typename _Rb_tree<_Key, _Val, _KeyOfValue, 20 _Compare, _Alloc>::iterator 21 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>:: 22 _M_upper_bound(_Link_type __x, _Link_type __y, 23 const _Key& __k) 24 { 25 while (__x != 0) 26 if (_M_impl._M_key_compare(__k, _S_key(__x))) 27 __y = __x, __x = _S_left(__x); 28 else 29 __x = _S_right(__x); 30 return iterator(__y); 31 }
stl(GCC4.9.3 in MinGW)中rbtree lower_bound/upper_bound 的实现
标签:
原文地址:http://www.cnblogs.com/lanyuliuyun/p/5617523.html