I do not want to use std::distance
because it will calculate whole distance from my iterator to the end. But I need to be sure that I have N or more elements fr
The implementation of RB_Tree::iterator
increment operator in libstd++
makes sure it would not return an iterator to an undefined memory location:
static _Rb_tree_node_base*
local_Rb_tree_increment(_Rb_tree_node_base* __x) throw ()
{
if (__x->_M_right != 0)
{
__x = __x->_M_right;
while (__x->_M_left != 0)
__x = __x->_M_left;
}
else
{
_Rb_tree_node_base* __y = __x->_M_parent;
while (__x == __y->_M_right)
{
__x = __y;
__y = __y->_M_parent;
}
if (__x->_M_right != __y)
__x = __y;
}
return __x;
}
But the same is not true for std::vector
:
#include
#include
#include
int main() {
std::vector vec = {1,2};
auto it = vec.begin();
it = std::next(it, 5);
if (it != vec.end()) {
std::cout << "Not end..go on" << std::endl;
}
return 0;
}
This will go on printing the message..
So, since the behaviour is not same across containers, you should not depend on std::next
for its current correct behaviour for map
based containers.