5.2.21 · HinglishC++ Programming

Iterators — input, output, forward, bidirectional, random access, contiguous

1,990 words9 min readRead in English

5.2.21 · Coding › C++ Programming


Iterators exist kyun karte hain?


Chha categories (WHAT)

Figure — Iterators — input, output, forward, bidirectional, random access, contiguous

Compiler categories ka use kaise karta hai (tag dispatch)

template<class It>
void advance_impl(It& it, int n, std::random_access_iterator_tag) {
    it += n;                       // O(1) — only legal here
}
template<class It>
void advance_impl(It& it, int n, std::bidirectional_iterator_tag) {
    if (n >= 0) while (n--) ++it;  // O(n)
    else        while (n++) --it;
}
template<class It>
void advance_impl(It& it, int n, std::input_iterator_tag) {
    while (n--) ++it;              // O(n), forward only
}
template<class It>
void advance(It& it, int n) {
    advance_impl(it, n,
        typename std::iterator_traits<It>::iterator_category{});
}

Ye step kyun? Empty tag object ka type compile time par overload select karta hai — zero runtime cost, phir bhi har container ko uska optimal algorithm milta hai. Hierarchy ka poora point yahi hai.


Worked examples


Common mistakes (Steel-man + fix)


Active recall

Iterator kya hota hai, ek phrase mein?
Ek generalized pointer jo container par uniform traversal deta hai.
Chha iterator categories refinement order mein batao.
Input/Output → Forward → Bidirectional → RandomAccess → Contiguous.
Bidirectional, Forward ke upar kaunsa operation add karta hai?
Backward stepping --it.
RandomAccess, Bidirectional ke upar kaunse operations add karta hai?
it+n, it-n, it[n], it2-it1, ordering </>, sab O(1) mein.
Contiguous, RandomAccess ke upar kya extra guarantee add karta hai?
Elements ek contiguous memory block mein hote hain: &*(it+n) == &*it + n.
Kaun sa container sirf bidirectional iterators rakhta hai, random access nahi?
std::list (aur set, map bhi).
std::deque random-access kyun hai lekin contiguous nahi?
Uski memory alag-alag fixed-size chunks mein split hai, toh addresses linear nahi hain.
"Single-pass" ka matlab kya hai aur kaunsi categories single-pass hain?
Har element sirf ek baar visit ho sakta hai; purani positions invalid ho jaati hain. Input aur Output iterators.
Ek hi naam se std::advance vectors ke liye O(1) aur lists ke liye O(n) kaise karta hai?
iterator_traits::iterator_category par tag dispatch compile time par overload choose karta hai.
Input iterator ka example? Output iterator ka?
istream_iterator; ostream_iterator / back_insert_iterator.
std::sort, std::list par kyun kaam nahi karta?
sort ko random-access iterators chahiye; list ke iterators bidirectional hain — list::sort() use karo.
Forward, input ke upar kya category add karta hai?
Multi-pass: aap ek iterator copy karke re-traverse kar sakte ho.
Recall Feynman: 12-saal ke bacche ko samjhao

Socho alag-alag toy boxes hain. Ek kagaz ki scroll jo sirf ek baar padhte waqt unroll hoti hai — woh input iterator hai. Ek stamp jo sirf print karta hai, kabhi padhta nahi — woh output hai. Ek train jo ek-taraf ki track par hai aur shuru se dobara chadhi ja sake, woh forward hai. Ek train jo reverse bhi kar sake, woh bidirectional hai. Ek teleporter jo seedha seat #50 par jump kare, woh random access hai. Aur agar saari seats ek seedhi line mein bina kisi gap ke jadi hain, toh woh contiguous hai. Jitna fancy toy, utna zyada kar sakta hai — aur STL woh sabse sasta trick choose karta hai jo toy allow karta hai.


Connections

  • Pointers and pointer arithmetic — iterators inhe generalize karte hain.
  • STL Containers — vector, list, deque, map — har ek specific iterator category expose karta hai.
  • STL Algorithms — sort, find, copy — iterator categories ke upar likhe gaye hain.
  • Templates and Tag Dispatchadvance/distance ke peeche compile-time mechanism.
  • iterator_traits and type introspection — category tag kaise padha jaata hai.
  • Ranges library (C++20) — iterator concepts ke upar bana modern layer.

Concept Map

solves

collapses to

classified by

form

refined by

refined by

adds --it

adds it+n O 1

adds contiguous storage

starts at

guarantees

Iterator = generalized pointer

M x N glue problem

M + N via common interface

Iterator categories

Refinement chain

Input read-only single-pass

Forward multi-pass

Output write-only single-pass

Bidirectional

Random Access

Contiguous

Elements in one memory block