Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Q&A When to use custom iterators versus pointers

However, I suspect that most iterators can be written as iterating over an array. From my experience, I wouldn't expect this to be the case. From the standard library containers, only std::vec...

posted 2mo ago by Angew‭

Answer
#1: Initial revision by user avatar Angew‭ · 2024-04-29T13:05:27Z (about 2 months ago)
 > However, I suspect that most iterators can be written as iterating over an array.

From my experience, I wouldn't expect this to be the case. From the standard library containers, only `std::vector` and `std::array` can work with iterators implemented as pointers. All the other containers have internal structures different from a C-style array.

After all, a pointer is by definition a `contiguous_iterator`, and most containers (not just `std` ones) are structured such that they don't (and can't) support contiguous iterators.

I've written many various iterator classes; mostly over ranges, but some over containers too. Not once was there a single contiguous array in the underlying the data structure such that a pointer could be used in place of the iterator.

> Is there any reason to wrap an iterator for array-like classes in a custom iterator class?

Nothing major. If I was writing a container/range that could use a pointer as an iterator, I'd probably use it and invest the saved development time elsewhere.

One possible reason to wrap a pointer in a class is that you can introduce custom behaviour, such as `assert`-based bounds checking which can catch some bugs in debug builds without slowing down production code.

> If yes, is there any class in the standard library that implements this?

I am not aware of anything in the standard library. However, when writing your own iterators, it's well worth it to look into the [Boost.Iterator](https://www.boost.org/doc/libs/1_85_0/libs/iterator/doc/index.html) library; in particular, you should consider checking out the [`boost::iterator_facade` class template](https://www.boost.org/doc/libs/1_85_0/libs/iterator/doc/iterator_facade.html).