Really just special types of pointers for things like vectors, arrays, and other iterables.

They can be derefenced to receover the value at the memory address. They are memory addresses. they can be increased, akin to a ‘nodenext :: node’ in a linekd list.

Using C++ Iterator
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
random_shuffle(v.begin(), v.end());

Declaring the iterator

set::iterator it = s.begin();
// OR Simpler/Shorter
auto it = s.begin();`

The element to which an iterator points can be accessed using the * symbol. For example, the following code prints the first element in the set:

auto it = s.begin(); cout << *it << "\n";

Iterators can be moved using the operators ++ (forward) and — (backward).

//Prints all the elements in increasing order using iteratorfor (auto it = s.begin(); it != s.end(); it++) { cout << *it << "\n"; }

Iterators vs Integer Indexing

There are two ways to traverse an iterable type (vectors, maps, sets, etc) the first one is with iterators:

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

Or with range-based for loops:

for (int val : v) { 
	std::cout << val << "\n"; 
}
Use CaseIterators ✅Integer Indices ✅
GeneralityWork with all standard containers (list, set, map, etc.)Only valid with containers that support indexing like vector, array
STL AlgorithmsRequired by most <algorithm> functions like std::sort, std::findNot compatible with STL algorithms directly
FlexibilityBidirectional/random access for easy navigationLimited to manual control over position
SafetyLess prone to off-by-one or invalid access bugsManual index management can be error-prone
Need the indexNot as straightforward; need std::distance or counterDirect access via v[i]; easy to compare positions like v[i] vs v[i+1]
Frequent random accessCan be verbose or awkward for arbitrary accessClean and fast random access (v[i])
In-place modificationsMore awkward for modifying specific positionsPrecise control over exact elements to change

⚖️ Which is “better”?

  • Iterator style: More idiomatic C++ for general container traversal, especially in modern C++

  • Index style: Better for numerical, performance-heavy code or when indexing logic is important.