#define _STLP_TEMPLATE_FOR_CONT_EXT template
_STLP_TEMPLATE_FOR_CONT_EXT
_Base_ptr _M_lower_bound(const _KT& __k) const {
_Base_ptr __y = __CONST_CAST(_Base_ptr, &this->_M_header._M_data); /* Last node which is not less than __k. */
_Base_ptr __x = _M_root(); /* Current node. */
while (__x != 0)
if (!_M_key_compare(_S_key(__x), __k))
__y = __x, __x = _S_left(__x);
else
__x = _S_right(__x);
return __y;
}
_STLP_TEMPLATE_FOR_CONT_EXT
_Base_ptr _M_upper_bound(const _KT& __k) const {
_Base_ptr __y = __CONST_CAST(_Base_ptr, &this->_M_header._M_data); /* Last node which is greater than __k. */
_Base_ptr __x = _M_root(); /* Current node. */
while (__x != 0)
if (_M_key_compare(__k, _S_key(__x)))
__y = __x, __x = _S_left(__x);
else
__x = _S_right(__x);
return __y;
}
_M_lower_bound 函数的功能是找出可以插入键__k的第一个位置,而_M_upper_bound是找出可以插入键__k的最后一个位置,如果原来队列中不存在__k,那么两个函数返回位置是一致的。
如果这两个函数应用在线性表上,那么其情形应该如下图所示。
在树中,情况会稍微复杂一点,但是不难理解,有图有真相。