Alternative C ++ alternatives reverse_iterator

I am trying to write a two-pass algorithm involving some old code. I want to move through a specific container twice, once in order and once in reverse order. Obviously, my first thought was to use iteratorand reverse_iterator, but it is strange that the developers of the container class that I use are not suitable for determining the working reverse_iteratorfor the container ( reverse_iteratorsit cannot be dereferenced here as it iteratorsmay be). I already have an algorithm that requires reverse_iterator.

My thought is to use the first pass iterator for the first part of the algorithm, and when I execute the push_frontelements algorithm in a new container and then iterate over the new container. This will require memory, which does not really matter in my application, but it made me think: are there any cleaner alternatives reverse_iteratorsin C ++, or should I spend time processing my algorithm using only forward iterators?

+3
source share
1 answer

If you need to iterate over the elements of a container in reverse order, you do not have to use a reverse iterator.

, --it end() begin() ++it begin() end().

, std::reverse_iterator ( ++ -- , ).

, , , , .

, , ; STL ++.

+4

Source: https://habr.com/ru/post/1775948/


All Articles