我正在iterator
通过 a 前后移动 a vector
。
我可以检查迭代器是否像这样运行结束:
++my_iterator;
if ( my_iterator == my_vector.end() )
{
--my_iterator; // if I want to stop the iterator at the end.
my_iterator = my_vector.begin(); // if I want the iterator to wraparound.
}
但是我如何检查它是否从一开始就跑了?
编辑:那我可以这样做吗?
--my_iterator;
if ( my_iterator == my_vector.rend() )
{
my_iterator = my_vector.begin(); // if I want to stop the iterator at the beginning.
my_iterator = --(my_vector.rbegin()); // if I want the iterator to wraparound.
}
还是我必须这样做?
std::vector< T >::iterator temp_reverse_iterator = reverse_iterator< T >( my_iterator );
++temp_reverse_iterator;
if ( temp_reverse_iterator == my_vector.rend() )
{
my_iterator = my_vector.begin(); // if I want to stop the iterator at the beginning.
my_iterator = --(my_vector.end()); // if I want the iterator to wraparound.
}
else
{
my_iterator = temp_reverse_iterator.base(); // do I need to -- this?
}
这两个例子在逻辑上都合理吗?