Iterator Categories input forward bidirectional random
Tell the iterator categories apart and pick algorithms that fit them.
Iterator Categories input forward bidirectional random is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is an Iterator?
An iterator is a generalized pointer. Algorithms work through iterators, decoupling them from the underlying container.
Five Iterator Categories
Iterators are classified by capability:
- Input — read-only, single pass
- Output — write-only, single pass
- Forward — read/write, multi pass
- Bidirectional — can move forward and backward
- Random Access — can jump by an integer offset
Input Iterators
Read once, advance, repeat. Cannot revisit. Example: std::istream_iterator.
std::istream_iterator<int> in(std::cin), end;
while (in != end) {
std::cout << *in << " ";
++in;
}Output Iterators
Write only. Example: std::ostream_iterator and std::back_inserter.
std::ostream_iterator<int> out(std::cout, " ");
std::vector<int> v = {1, 2, 3};
std::copy(v.begin(), v.end(), out);Forward Iterators
Like input but multi pass — you can re-iterate from the same position. Example: std::forward_list.
Bidirectional Iterators
Forward iterators plus --. Example: std::list, std::map, std::set.
std::list<int> l = {1, 2, 3};
auto it = l.end();
--it; // OK, points to last elementRandom Access Iterators
Bidirectional plus jumps: it + n, it - n, it[k], it1 - it2. Example: std::vector, std::deque, raw arrays.
Algorithm Requirements
Each algorithm specifies the minimum iterator category it needs. std::sort requires random access; std::find requires only input.
Iterator Traits
std::iterator_traits<Iter> exposes the iterator s value type, category, and other properties at compile time.
using Category = std::iterator_traits<It>::iterator_category;
using Value = std::iterator_traits<It>::value_type;std::advance and std::distance
Move an iterator or measure a distance generically. They use the category for the most efficient implementation.
auto it = v.begin();
std::advance(it, 5); // efficient on random access, O(n) otherwise
auto d = std::distance(v.begin(), it);Custom Iterators
To write your own, define the required type aliases and operations for the category you target — operator*, operator++, operator==, etc.
C++20 Iterator Concepts
C++20 added formal concepts like std::input_iterator, std::forward_iterator, and std::random_access_iterator. They replace the older tag-based system.
Quick Check
Which iterator category supports the expression it + n in constant time?
Recap
Iterators are categorized by capability — input, output, forward, bidirectional, random access. Algorithms require a minimum category. Vector iterators are random access; list and map are bidirectional.
Frequently asked questions
Is the “Iterator Categories input forward bidirectional random” lesson free?
Yes — the full text of “Iterator Categories input forward bidirectional random” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Iterator Categories input forward bidirectional random”?
Tell the iterator categories apart and pick algorithms that fit them. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Iterator Categories input forward bidirectional random” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Iterator Categories input forward bidirectional random
- Common Iterator Patterns begin end advance
- C++20 Ranges Library Introduction
- Range Adaptors views::filter transform take