Iterator library
Iterator concepts, traits, adaptors, range-access utilities, and the boundary between classic iterators and ranges.
The iterator library is the bridge between algorithms and data structures: it defines how code traverses, reads, writes, advances through, and adapts sequences. This hub is the curated starting point when the question is about iterator capabilities, categories, utilities, or adaptor types rather than a specific container or ranges view.
# Start Here
Iterator concepts and traversal strength
Start here when you need to know what an iterator is allowed to do: single-pass input, multi-pass forward, bidirectional movement, random access, or contiguous storage guarantees.
Traits and associated types
Use the traits route when generic code needs `value_type`, `difference_type`, tags, or pointer-like adaptation across iterator types.
Iterator adaptors
Start here when you need to wrap an existing iterator to reverse, move, insert, count, or normalize sentinel/iterator pairs.
Navigation helpers
Use these utilities when you already have iterators and need to move them, measure distance, or obtain begin/end/data/size-style access uniformly.
# Quick Map
| If you need to... | Start with | Why |
|---|---|---|
| Describe how far an iterator can move and what guarantees it has | concepts, iterator tags | This is the route for capability modeling: readable vs writable, single-pass vs multi-pass, random access vs contiguous layout. |
| Write generic code against iterator-associated types | iterator_traits, incrementable_traits | Traits provide the canonical metadata and defaults that algorithms and generic adapters rely on. |
| Reverse or move through an existing iterator interface | reverse_iterator, move_iterator | These adaptors change traversal or value category behavior without requiring a different underlying container. |
| Insert through an iterator facade into a container | insert_iterator, back_insert_iterator, front_insert_iterator | Insertion adaptors are the canonical output-iterator route for algorithms that write into containers. |
| Advance iterators, compute distances, or get neighboring positions | advance, distance, next, prev | These helpers abstract over traversal strength while preserving the iterator model. |
| Access a range through non-member `begin`/`end`/`data`/`size` utilities | begin, end, data, size, empty | These are the classic range-access endpoints that bridge arrays, containers, and some view-like types. |
| Use iterators backed by streams rather than containers | istream_iterator, ostream_iterator, istreambuf_iterator | Stream iterators connect iterator-oriented algorithms to formatted or buffer-level I/O. |
| Navigate the modern ranges-based iterator/sentinel world | ranges iterator utilities, Ranges hub | Ranges reuse iterator ideas but add sentinels, CPOs, and view-oriented navigation beyond the classic iterator page split. |
# Iterator Strength Ladder
| Concept | Main guarantee | Use it for |
|---|---|---|
| input_iterator | Readable, single-pass traversal | Basic read pipelines, streaming inputs, and sentinel-based one-pass algorithms. |
| output_iterator | Writable output progression | Algorithm sinks and iterator-based output destinations. |
| forward_iterator | Multi-pass readable traversal | Stable traversal where revisiting positions is meaningful. |
| bidirectional_iterator | Can move backward as well as forward | Reverse traversal and predecessor-based algorithms. |
| random_access_iterator | Constant-time jumps and distance | Index-like traversal, arithmetic on iterators, and random-access algorithms. |
| contiguous_iterator | Elements occupy contiguous memory | APIs that need raw pointers or contiguous storage guarantees. |
# Iterator Families
Traits and tags
Type metadata, iterator categories, difference types, and machinery that generic algorithms inspect.
Traversal adaptors
Adaptors that change how an underlying iterator is traversed or interpreted.
reverse_iterator · move_iterator · counted_iterator · common_iterator
Insertion adaptors
Output-iterator facades that write into containers via insert, push_back, or push_front semantics.
insert_iterator · back_insert_iterator · front_insert_iterator
Stream iterators
Iterator-shaped interfaces for stream extraction/insertion and stream-buffer-level traversal.
istream_iterator · ostream_iterator · istreambuf_iterator · ostreambuf_iterator
Range access helpers
Non-member utility functions that discover iteration endpoints, storage pointers, or size/emptiness information.
Ranges iterator utilities
The CPO-based, sentinel-friendly iterator utilities that live on the boundary between classic iterators and the ranges library.
# Utility Operations
| Operation | Use it for | Primary destinations |
|---|---|---|
| Advance / next / prev | Moving iterators without manually spelling out repeated increment/decrement logic. | advance, next, prev |
| Distance | Measuring how far apart two positions are according to iterator strength. | distance, ranges::distance |
| Begin/end/data/size/empty | Uniform access to endpoints and shape information for arrays, containers, and some range-like types. | begin, end, data, size, empty |
| Iterator-sentinel normalization | Bridging differing iterator/sentinel forms into algorithm-friendly shapes. | common_iterator, default_sentinel_t, unreachable_sentinel_t |
# Boundary Lines
| This hub covers | Use a different hub for |
|---|---|
| Iterator concepts, traits, adaptors, stream iterators, and endpoint/navigation utilities. | Ranges for view pipelines and CPO-heavy range navigation, Containers for choosing data structures, and Algorithms for task-oriented operation selection. |
| Classic iterator-based traversal surfaces. | Ranges when the code is primarily range/view/sentinel-first instead of iterator-pair-first. |
# Practical Routes
I need to specify iterator requirements
Start from iterator concepts when the job is to constrain templates or understand what a generic algorithm may legally assume.
I need to adapt an existing iterator
Use iterator adaptors when you want a different traversal or insertion behavior without designing a new container interface.
I need common iterator operations
Use the classic utilities first when the problem is traversal math, endpoint discovery, or generic access helpers over existing ranges.