Files
2026-05-04 08:12:20 +08:00

120 lines
3.5 KiB
Org Mode

* What is a C++ iterator :cpp:iterator:concept:recognition:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1776621491263
:END:
** 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:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1776621491259
:END:
** Front
Write C++ code to iterate through a std::vector<int> using iterators
** Back
#+begin_src c++
std::vector<int> vec = {1, 2, 3};
for (auto it = vec.begin(); it != vec.end(); ++it) {
std::cout << *it << " ";
}
#+end_src
* Iterator operations :cpp:iterator:concept:enumeration:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1776621491255
:END:
** 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:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1776621491250
:END:
** Front
List the five iterator categories in C++ (from simplest to most advanced)
** Back
:PROPERTIES:
:ANKI_NOTE_ID: 1777826854918
:END:
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:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1776621491246
:END:
** Front
Write C++ code to safely traverse a const std::vector without modifying elements
** Back
#+begin_src c++
std::vector<int> vec = {1, 2, 3};
for (auto it = vec.cbegin(); it != vec.cend(); ++it) {
std::cout << *it << " ";
}
#+end_src
* Task: Use reverse iterator :cpp:iterator:production:code:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1776621491241
:END:
** Front
Write C++ code to iterate backwards through a std::vector
** Back
#+begin_src c++
std::vector<int> vec = {1, 2, 3};
for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
std::cout << *it << " ";
}
#+end_src
* Minimal custom iterator implementation :cpp:iterator:implementation:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1776621491237
:END:
** Front
Write the minimal C++ code to implement a forward iterator for a custom class
** Back
#+begin_src c++
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); }
};
#+end_src
* When to use iterators vs indices :cpp:iterator:concept:comparison:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1776621491226
:END:
** 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