C++ Iterator Quiz
40 in-depth questions covering C++ iterator fundamentals including iterator categories, STL container usage, begin/end patterns, reverse iterators, and iterator invalidation rules — with 16 code examples to master iterator-based programming.
Question 1
What is an iterator in C++?
Question 2
What are the five fundamental iterator categories in C++?
Question 3
What is the difference between input and output iterators?
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Output iterator (back_inserter)
std::copy(vec.begin(), vec.end(),
std::back_inserter(vec)); // Can only write
// Input iterator (istream_iterator)
// std::copy(std::istream_iterator<int>(std::cin),
// std::istream_iterator<int>(),
// std::back_inserter(vec)); // Can only read
return 0;
}Question 4
What capabilities does a forward iterator provide?
Question 5
What additional capabilities does a bidirectional iterator provide over forward iterators?
#include <list>
#include <algorithm>
int main() {
std::list<int> lst = {1, 2, 3, 4, 5};
// Bidirectional iterator can go backward
auto it = std::find(lst.begin(), lst.end(), 3);
if (it != lst.end()) {
--it; // Move backward - only possible with bidirectional
std::cout << *it << std::endl; // Prints 2
}
return 0;
}Question 6
What capabilities does a random access iterator provide?
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Random access iterator arithmetic
auto it = vec.begin();
std::advance(it, 3); // Move forward by 3
std::cout << *it << std::endl; // 4
auto it2 = it + 1; // Iterator arithmetic
std::cout << *it2 << std::endl; // 5
if (it2 > it) { // Iterator comparison
std::cout << "it2 is after it" << std::endl;
}
return 0;
}Question 7
What iterator category does std::vector provide?
Question 8
What iterator category does std::list provide?
Question 9
What iterator category do std::set and std::map provide?
Question 10
What is the purpose of begin() and end() member functions?
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// begin() returns iterator to first element
auto first = vec.begin();
std::cout << *first << std::endl; // 1
// end() returns iterator to one-past-last element
auto last = vec.end();
--last; // Move to actual last element
std::cout << *last << std::endl; // 5
// Range-based algorithms
std::sort(vec.begin(), vec.end());
return 0;
}Question 11
What is a reverse iterator and how does it work?
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Reverse iterator from rbegin()
auto rit = vec.rbegin();
std::cout << *rit << std::endl; // 5 (last element)
++rit;
std::cout << *rit << std::endl; // 4 (moving backward)
// Convert to forward iterator
auto fit = rit.base(); // Points to element after *rit
std::cout << *fit << std::endl; // 5
return 0;
}Question 12
What is iterator invalidation and why is it dangerous?
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
auto it = vec.begin() + 2; // Points to element 3
std::cout << *it << std::endl; // 3
vec.push_back(6); // May reallocate and invalidate iterators
// it is now INVALID - undefined behavior!
// std::cout << *it << std::endl; // DANGER!
return 0;
}Question 13
What operations can invalidate iterators in std::vector?
Question 14
What operations can invalidate iterators in std::list?
Question 15
What operations can invalidate iterators in std::map and std::set?
Question 16
What is the difference between const_iterator and iterator?
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Regular iterator - read/write access
std::vector<int>::iterator it = vec.begin();
*it = 10; // Can modify
std::cout << *it << std::endl;
// Const iterator - read-only access
std::vector<int>::const_iterator cit = vec.cbegin();
// *cit = 20; // Error: cannot modify through const_iterator
std::cout << *cit << std::endl;
// Auto with const container
const std::vector<int> cvec = {1, 2, 3};
auto cit2 = cvec.begin(); // const_iterator automatically
return 0;
}Question 17
What is a range-based for loop and how does it use iterators?
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Range-based for loop (uses iterators internally)
for (int& value : vec) {
value *= 2;
}
// Equivalent to:
// for (auto it = vec.begin(); it != vec.end(); ++it) {
// int& value = *it;
// value *= 2;
// }
for (int value : vec) {
std::cout << value << " ";
}
return 0;
}Question 18
What is std::advance and when should it be used?
#include <vector>
#include <list>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::list<int> lst = {1, 2, 3, 4, 5};
// For random access iterators (vector)
auto vit = vec.begin();
std::advance(vit, 3); // Efficient O(1)
std::cout << *vit << std::endl; // 4
// For bidirectional iterators (list)
auto lit = lst.begin();
std::advance(lit, 3); // O(n) - less efficient
std::cout << *lit << std::endl; // 4
return 0;
}Question 19
What is std::distance and how does it work with different iterator categories?
#include <vector>
#include <list>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::list<int> lst = {1, 2, 3, 4, 5};
// Random access (efficient)
auto dist1 = std::distance(vec.begin(), vec.end());
std::cout << dist1 << std::endl; // 5, O(1)
// Bidirectional (less efficient)
auto dist2 = std::distance(lst.begin(), lst.end());
std::cout << dist2 << std::endl; // 5, O(n)
return 0;
}Question 20
What is an iterator adaptor and what are some common examples?
Question 21
What is the difference between std::back_inserter and std::front_inserter?
#include <vector>
#include <deque>
#include <algorithm>
int main() {
std::vector<int> source = {1, 2, 3};
std::vector<int> dest1;
std::deque<int> dest2;
// back_inserter adds to end
std::copy(source.begin(), source.end(),
std::back_inserter(dest1));
// dest1 now: {1, 2, 3}
// front_inserter adds to beginning
std::copy(source.begin(), source.end(),
std::front_inserter(dest2));
// dest2 now: {3, 2, 1} (reverse order)
return 0;
}Question 22
What is the difference between pre-increment and post-increment for iterators?
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
auto it = vec.begin();
// Pre-increment: increment then use
auto val1 = *++it; // it now points to 2, val1 = 2
std::cout << val1 << std::endl;
// Reset iterator
it = vec.begin();
// Post-increment: use then increment
auto val2 = *it++; // val2 = 1, it now points to 2
std::cout << val2 << std::endl;
return 0;
}Question 23
What is an iterator trait and why is it useful?
#include <iterator>
#include <vector>
#include <iostream>
template<typename Iter>
void advance_iterator(Iter& it, typename std::iterator_traits<Iter>::difference_type n) {
// Use traits to get the difference type
using diff_t = typename std::iterator_traits<Iter>::difference_type;
if constexpr (std::is_same_v<typename std::iterator_traits<Iter>::iterator_category,
std::random_access_iterator_tag>) {
it += n; // Efficient for random access
} else {
// Manual advancement for other categories
for (diff_t i = 0; i < n; ++i) ++it;
}
}
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
auto it = vec.begin();
advance_iterator(it, 3);
std::cout << *it << std::endl; // 4
return 0;
}Question 24
What is the difference between iterator and const_iterator typedefs?
Question 25
What is the difference between std::cbegin() and std::begin()?
Question 26
What is the difference between std::next() and std::advance()?
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
auto it = vec.begin();
// advance modifies the iterator in-place
std::advance(it, 2);
std::cout << *it << std::endl; // 3
// next returns a new iterator, original unchanged
auto it2 = std::next(it, 2);
std::cout << *it << std::endl; // Still 3
std::cout << *it2 << std::endl; // 5
return 0;
}Question 27
What is the difference between std::prev() and manual decrement?
Question 28
What is the difference between iterator tags and how are they used?
Question 29
What is the difference between std::make_reverse_iterator() and rbegin()?
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Container's rbegin()
auto rit1 = vec.rbegin();
std::cout << *rit1 << std::endl; // 5
// make_reverse_iterator from forward iterator
auto rit2 = std::make_reverse_iterator(vec.end());
std::cout << *rit2 << std::endl; // 5 (same result)
return 0;
}Question 30
What is the difference between iterator invalidation in debug vs release builds?
Question 31
What is the difference between std::inserter and std::back_inserter?
#include <vector>
#include <set>
#include <algorithm>
int main() {
std::vector<int> source = {1, 2, 3};
std::set<int> dest;
// inserter calls insert() at specified position
std::copy(source.begin(), source.end(),
std::inserter(dest, dest.end()));
// dest now: {1, 2, 3}
// back_inserter calls push_back()
std::vector<int> dest2;
std::copy(source.begin(), source.end(),
std::back_inserter(dest2));
// dest2 now: {1, 2, 3}
return 0;
}Question 32
What is the difference between iterator movement and element access?
Question 33
What is the difference between std::end() and std::cend()?
Question 34
What is the difference between iterator ranges and iterator pairs?
Question 35
What is the difference between iterator stability and iterator validity?
Question 36
What is the difference between std::move_iterator and regular iterators?
#include <vector>
#include <algorithm>
int main() {
std::vector<std::string> source = {"hello", "world", "test"};
std::vector<std::string> dest;
// Regular copy
std::copy(source.begin(), source.end(),
std::back_inserter(dest));
// dest has copies of strings
// Move iterator for efficient transfer
std::vector<std::string> dest2;
std::copy(std::make_move_iterator(source.begin()),
std::make_move_iterator(source.end()),
std::back_inserter(dest2));
// dest2 has moved-from strings, source may be in valid but unspecified state
return 0;
}Question 37
What is the difference between iterator debugging and iterator checking?
Question 38
What is the difference between iterator position and iterator offset?
Question 39
What is the difference between iterator lifetime and iterator scope?
Question 40
What is the fundamental principle for safe iterator usage across different STL containers?
