Questions tagged [reverse-iterator]

Related specifically to iterators that traverses a container in reverse order. An iterator is an object-oriented programming pattern that allows traversal through a collection, agnostic of the actual implementation or object addresses in physical memory.

82 questions
21
votes
2 answers

Why use rbegin() instead of end() - 1?

I'm wondering what the benefits of using rbegin() rather than end() - 1 are for STL containers. For example, why would you use something like: vector v; v.push_back(999); vector::reverse_iterator r = v.rbegin(); vector::iterator i =…
Victor Brunell
  • 5,668
  • 10
  • 30
  • 46
20
votes
2 answers

What's the difference between a reversed tuple and a reversed list?

Reversing a tuple and reversing a list returns objects of different type: >>> reversed((1,2)) >>> reversed([1,2]) They have the same dir. Neither type is a subclass of the…
wim
  • 338,267
  • 99
  • 616
  • 750
19
votes
3 answers

Get index in vector from reverse iterator

I know how to get the index from a vector iterator, by subtracting the begin iterator from it. For example: vector::iterator it = find(vec.begin(), vec.end(), x); size_t position = it - vec.begin(); However, now I want to find the index of the…
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
17
votes
4 answers

Exposition only in the C++ standard?

What does exposition only exactly means in the C++ standard? Does it mean that the private/protected members marked exposition only are required to exist by standard, or are they just a "suggestion" of implementation, and are not required at…
Vincent
  • 57,703
  • 61
  • 205
  • 388
14
votes
1 answer

How do you use find_if along with reverse_iterator on a C-style array?

To search the first occurence of a element in a C-Array with POD elements, one ease can do this with std::find_if(begin, end, findit). But I needed the last occurence. This answer gave me the idea that this can be done with std::reverse_iterator.…
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
13
votes
2 answers

Check if iterator's type is reverse_iterator

Is there a way to check if iterator passed as arg to fnc is reverse_iterator? Are there any iterator traits function I could use?
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
13
votes
3 answers

Is it safe to use std::prev(vector.begin()) or std::next(vector.begin(), -1) like some_container.rend() as reversed sentry?

I wrote some code that takes iterators but have to do comparison in reversed order, template bool func(ConstBiIter seq_begin, ConstBiIter seq_end) { ConstBiIter last = std::prev(seq_end); while (--last !=…
dguan
  • 1,023
  • 1
  • 9
  • 21
12
votes
1 answer

One variable for either iterator or reverse_iterator?

I want to iterate through some std::vectors in a for loop but depending on some conditions, the vectors shall be iterated either forward or backward. I thought, I could easily do it by using either normal iterators or reverse iterators like…
Sven Hoek
  • 170
  • 9
11
votes
1 answer

Erasing element from a vector – rbegin() vs begin()

I'm trying to solve a problem in C++, a part of which requires me to erase elements from a vector using the rbegin() member function. However, the compiler throws an error every time I write the below-mentioned code. What's wrong here? int main()…
RedHelmet
  • 213
  • 1
  • 7
11
votes
2 answers

Why does reverse_iterator doubly define its nested types?

It appears that the iterator adaptor reverse_iterator doubly defines most of its nested types. In particular, it inherits publicly from std::iterator which exposes iterator_category, value_type, difference_type, pointer and reference. Except for…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
11
votes
1 answer

Why does removing the _first_ element of a list invalidate `.rend()`?

Tested on Mac OS X using XCode 4.6. This example code shows removing the last element of an std::list works as I expected: an iterator reference to list::end() is still "1 past the end" and is still valid, even through removal of the last…
bobobobo
  • 64,917
  • 62
  • 258
  • 363
10
votes
1 answer

reverse_iterator weird behavior with 2D arrays

I have a 2D array. It's perfectly okay to iterate the rows in forward order, but when I do it in reverse, it doesn't work. I cannot figure out why. I'm using MSVC v143 and the C++20 standard. int arr[3][4]; for (int counter = 0, i = 0; i != 3; ++i)…
Seideun
  • 153
  • 2
  • 7
10
votes
1 answer

Reverse iteration in Julia

Yesterday I had occasion to want to iterate over a collection in reverse order. I found the reverse function, but this does not return an iterator, but actually creates a reversed collection. Apparently, there used to be a Reverse iterator, which…
DNF
  • 11,584
  • 1
  • 26
  • 40
10
votes
3 answers

STL iterator before std::map::begin()

In C++11's std::map, is there some valid iterator x such that ++x is guaranteed to equal map::begin()? I would like to detect if a function I just called (mine) has walked an iterator off the front of a function. The function will move the…
George Hilliard
  • 15,402
  • 9
  • 58
  • 96
7
votes
1 answer

What C++20 change to reverse_iterator is breaking this code?

The following code compiles in C++11, C++14, and C++17, but does not compile in C++20. What change to the standard broke this code? #include #include template struct bar { typename T::reverse_iterator…
Pubby
  • 51,882
  • 13
  • 139
  • 180
1
2 3 4 5 6