Last Updated: December 6, 2025
30 quizzes
Which operation must every C++ iterator type support at minimum?
What do begin() and end() of a standard container typically return?
Which iterator category allows both ++ and --, but not random jumps?
Which standard container provides random access iterators?
Which is a key advantage of using iterators with STL algorithms?
Which standard component is an example of an output iterator adapter?
What does std::make_reverse_iterator do?
When implementing a custom random access iterator, which typedef or using alias is critical for STL compatibility?
Which statement about input iterators is correct?
Which is the safest way to write through an iterator into a std::vector<int>?
Get a const iterator to the beginning of a std::vector<int> v
auto it = v.();Click an option to fill the blank:
Use std::back_inserter to append elements to dst from src
std::copy(src.begin(), src.end(), std::(dst));Click an option to fill the blank:
Create a transform iterator that doubles elements of vec using a lambda
auto it = std::make_transform_iterator(vec.begin(), (int x){ return x * 2; });Click an option to fill the blank:
What is the output of this code using a vector iterator?
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> v{1, 2, 3};
6 auto it = v.begin();
7 *it = 10;
8 ++it;
9 std::cout << v[0] << " " << *it << " " << v.back() << "\n";
10 return 0;
11}What does this reverse iteration print?
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> v{1, 2, 3, 4};
6 for (auto it = v.rbegin(); it != v.rend(); ++it) {
7 std::cout << *it;
8 }
9 std::cout << "\n";
10 return 0;
11}What is the output when using std::transform with back_inserter?
1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main() {
6 std::vector<int> src{1, 2, 3};
7 std::vector<int> dst;
8 std::transform(src.begin(), src.end(), std::back_inserter(dst),
9 [](int x){ return x * x; });
10 for (int x : dst) std::cout << x << " ";
11 std::cout << "\n";
12 return 0;
13}What is the output of this code using a custom-like pointer iterator?
1#include <iostream>
2
3int main() {
4 int arr[] = {5, 10, 15};
5 int* begin = arr;
6 int* end = arr + 3;
7 for (int* it = begin; it != end; ++it) {
8 if (it == begin + 1) *it = 42;
9 }
10 std::cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
11 return 0;
12}What does this code print when mixing iterators and indices?
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> v{2, 4, 6, 8};
6 auto it = v.begin();
7 it += 2;
8 std::cout << *it << " ";
9 std::cout << v[it - v.begin()] << "\n";
10 return 0;
11}Find the bug in this iterator-based erase function
Click on the line(s) that contain the bug.
#include <vector> void remove_even(std::vector<int>& v) { for (auto it = v.begin(); it != v.end(); ++it) { if (*it % 2 == 0) { v.erase(it); } }}Find the bug in this custom iterator increment implementation
Click on the line(s) that contain the bug.
#include <cstddef> template<typename T>class RawPtrIterator { T* ptr_;public: explicit RawPtrIterator(T* p) : ptr_(p) {} T& operator*() const { return *ptr_; } RawPtrIterator& operator++() { // move to next element ptr_--; return *this; }};Click the line that dereferences an invalidated iterator
Click on the line to select.
#include <vector>#include <algorithm> int main() { std::vector<int> v{1,2,3,4,5}; auto it = std::find(v.begin(), v.end(), 3); v.push_back(6); if (it != v.end()) { std::cout << *it << "\n"; } return 0;}Click the line where a const_iterator is incorrectly modified
Click on the line to select.
#include <vector> int main() { std::vector<int> v{1,2,3}; std::vector<int>::const_iterator it = v.begin(); *it = 10; return 0;}Complete the range-based for loop using iterators under the hood
#include <vector>#include <iostream>int main() { std::vector<int> v{1,2,3}; for ( it = v.(); it != v.(); ++it) { std::cout << *it << " "; } std::cout << "\n";}Click an option to fill blank 1:
Fill in the blanks to use std::make_reverse_iterator correctly
#include <vector>#include <iostream>#include <iterator>int main() { std::vector<int> v{1,2,3}; auto rbegin = std::make_reverse_iterator(v.()); auto rend = std::make_reverse_iterator(v.()); for (auto it = rbegin; it != rend; ++it) { std::cout << *it << " "; } std::cout << "\n";}Click an option to fill blank 1:
Fill in the blanks to declare iterator traits for a custom iterator
struct MyIter { using = std::random_access_iterator_tag; using = int; using difference_type = std::ptrdiff_t; using pointer = int*; using reference = int&;};Click an option to fill blank 1:
Complete the code using std::istream_iterator and std::back_inserter
#include <vector>#include <iterator>#include <algorithm>void read_all(std::istream& is, std::vector<int>& v) { std::<int> first(is), last; std::copy(first, last, std::(v));}Click an option to fill blank 1:
Match each iterator category with a valid operation guarantee
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match iterator-related adapters with their description
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Order the typical steps to implement a simple custom forward iterator for a dynamic array
Drag and drop to reorder, or use the arrows.
Order the operations when using a transform iterator with std::vector<int>
Drag and drop to reorder, or use the arrows.