News for hackers. Stuff that matters.
Re:Writing a custom iterator in modern C++ by missblit on Monday December 21, 2020 @12:21AM (#25492333)
You can think of it as implementing `seek`, not just `next`.

`RandomAccessIterator` implements the `+=` and `-=` operations that `BidirectionalIterator` does not. Algorithms that accept `RandomAccessIterator` can then assume that calling += is possible.

Re:Writing a custom iterator in modern C++ by arcticbull on Monday December 21, 2020 @12:40AM (#25492410)
What's the advantage to using a random access iterator over a get() indexed? Like to me the point of an iterator is that it abstracts over access kind, and exposes the minimum viable view, which is sequential forward and possibly backward access.

If you want indexed access why wouldn't you just, you know, use anything that implements operator[]?

Re:Writing a custom iterator in modern C++ by missblit on Monday December 21, 2020 @12:58AM (#25492478)
Just that iterators is the API that C++ settled on for doing this in a generic way (well concepts in C++20, but I haven't learned that yet).

It's totally 100% kosher to write a function that calls `.at()` instead of `operator+=`. As long as it only needs to work on vectors, strings, or other stuff with a `.at()` method.

But if the function works on iterators instead it can work on ANY data structure that also fulfills the requirements of RandomAccessIterator API. String Views, raw pointers, a 3rd party Cord library, etc.

There's no inherent meaning to the iterator API requiring `operator+=` instead of something closer to `get()`, except that it means that the iterator itself doesn't need to know anything about container bounds (like I said above, raw pointers work as RandomAccessIterators).

Re:Writing a custom iterator in modern C++ by Too on Monday December 21, 2020 @06:00AM (#25493805)
[] alone doesn't imply that the view is sequential. Binary search for example requires both sequential items and indexed access. You can't binary search an std::map even if it implements [].
Re:Writing a custom iterator in modern C++ by josefx on Monday December 21, 2020 @06:42AM (#25493992)
You need an additional API to create views/sublists if you want to transparently operate on sub ranges and if you want to return a found index you may need to convert its value to apply to a parent view.