Files
cpp-flashcards/org/cpp/iterator.org
T
tomatocream ab80017903 feat: add iterator flashcards, sync script, and project scaffolding
- iterator.org: C++ iterator flashcard deck
- questions.org: LeetCode question index (217, 242)
- inbox.org: learning inbox with two-sum implementation and notes
- sync-anki.sh: batch sync org files to Anki via emacsclient
- AGENTS.md: conventions for AI agents working in this repo

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 00:48:31 +08:00

3.5 KiB
Raw Blame History

What is a C++ iterator   cpp iterator concept recognition

Front

What is an iterator in C++?

Back

An object that points to elements in a container and allows traversal through them, providing a common interface for accessing elements sequentially without exposing the underlying structure.

Task: Traverse a vector with iterators   cpp iterator production code

Front

Write C++ code to iterate through a std::vector<int> using iterators

Back

std::vector<int> vec = {1, 2, 3};
for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
}

Iterator operations   cpp iterator concept enumeration

Front

What are the four required operations for a C++ iterator?

Back

  • `operator*` — dereference to get current element
  • `operator++` — advance to next element (pre and post)
  • `operator==` — equality comparison
  • `operator!=` — inequality comparison

cpp iterator categories   cpp iterator concept enumeration

Front

List the five iterator categories in C++ (from simplest to most advanced)

Back

  1. Input Iterator — read values, advance once
  2. Output Iterator — write values, advance once
  3. Forward Iterator — read/write, multiple passes
  4. Bidirectional Iterator — + operator
  5. Random Access Iterator — + arithmetic, subscript

Task: Use const iterator   cpp iterator production code

Front

Write C++ code to safely traverse a const std::vector without modifying elements

Back

std::vector<int> vec = {1, 2, 3};
for (auto it = vec.cbegin(); it != vec.cend(); ++it) {
    std::cout << *it << " ";
}

Task: Use reverse iterator   cpp iterator production code

Front

Write C++ code to iterate backwards through a std::vector

Back

std::vector<int> vec = {1, 2, 3};
for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
    std::cout << *it << " ";
}

Minimal custom iterator implementation   cpp iterator implementation production

Front

Write the minimal C++ code to implement a forward iterator for a custom class

Back

class MyIterator {
    Node* ptr;
public:
    reference operator*() const { return ptr->data; }
    pointer operator->() const { return &ptr->data; }
    MyIterator& operator++() { ptr = ptr->next; return *this; }
    MyIterator operator++(int) { auto tmp = *this; ++*this; return tmp; }
    bool operator==(const MyIterator& other) const { return ptr == other.ptr; }
    bool operator!=(const MyIterator& other) const { return !(*this == other); }
};

When to use iterators vs indices   cpp iterator concept comparison

Front

When would you choose iterators over index-based loops in C++?

Back

  • Container type is unknown or could change (iterators abstract the container)
  • Using standard library algorithms (std::find, std::sort, etc.)
  • Need to traverse multiple containers simultaneously
  • Working with containers without random access (list, map)
  • Decouple algorithm from container structure