The C++ standard library
The C++ standard library's The header provides a set of tools for traversing elements in containers. An iterator is one of the core concepts in the C++ Standard Template Library (STL); it allows programmers to access elements in containers in a uniform way, without needing to care about the container's implementation details.
An iterator is an object that provides a way to traverse elements in a container. An iterator can be viewed as a pointer to an element in a container, but it is more flexible and powerful than a pointer. Iterators can be used to access and modify elements in a container, and can be used together with STL algorithms.
Iterators are mainly divided into the following categories:
- Input Iterator: Can only perform single-pass read operations, not write operations.
- Output Iterator: Can only perform single-pass write operations, not read operations.
- Forward Iterator: Can perform read and write operations, and can move forward.
- Bidirectional Iterator: In addition to all operations of a forward iterator, it can also move backward.
- Random Access Iterator: In addition to all operations of a bidirectional iterator, it also supports random access, such as accessing elements by index.
Commonly used functions (key points)
| function | Purpose | Example | Description |
|---|---|---|---|
std::advance(it, n) |
Move iterator n steps | advance(it, 2); |
Modifies the original iterator |
std::distance(a, b) |
Calculate the distance between two iterators | distance(v.begin(), v.end()); |
Returns the number of elements |
std::next(it, n) |
Returns a new iterator advanced n steps forward | auto it2 = next(it, 2); |
Recommended; does not modify the original value |
std::prev(it, n) |
Returns a new iterator advanced n steps backward | auto it2 = prev(it, 1); |
Supported since C++11 |
Iterator adapters (very important)
Iterator adapters can change an iterator's behavior to adapt it to different usage scenarios.
| Adapter | Purpose | Example |
|---|---|---|
std::back_inserter |
Tail insertion (calls push_back) | back_inserter(vec) |
std::front_inserter |
Head insertion (calls push_front) | front_inserter(list) |
std::inserter |
Insertion at a specified position | inserter(vec, it) |
Stream iterators (IO simplification tool)
| Type | Purpose | Example |
|---|---|---|
std::istream_iterator |
Reads data from an input stream | istream_iterator |
std::ostream_iterator |
Writes to an output stream | ostream_iterator |
Iterator syntax
The syntax of an iterator is usually as follows:
#include
// Iterate over the container using an iterator
for (ContainerType::iterator it = container.begin(); it != container.end(); ++it) {
// Access element *it
}
Example
Below is an example using Header file and iterator traversal std::vector example:
Example
#include
#include
#include
#include
int
main
(
)
{
// Create and initialize a vector container
std
::
vector
<
int
vec
=
{
1
,
2
,
3
,
4
,
5
};
// Traverse the vector using an iterator
for
(
std
::
vector
<
int
::
iterator
it
=
vec.
begin
(
)
;
it
!
=
vec.
end
(
)
;
++
it
)
{
std
::
cout
<<
it
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Use the auto keyword to simplify the iterator type
for
(
auto
it
=
vec.
begin
(
)
;
it
!
=
vec.
end
(
)
;
++
it
)
{
std
::
cout
<<
it
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Use the C++11 range-based for loop
for
(
int
elem
:
vec
)
{
std
::
cout
<<
elem
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Use back_inserter for automatic insertion
std
::
vector
<
int
v2;
std
::
fill_n
(
std
::
back_inserter
(
v2
)
,
3
,
100
)
;
// Use ostream_iterator for output
std
::
copy
(
v2.
begin
(
)
, v2.
end
(
)
,
std
::
ostream_iterator
<
int
(
std::
cout
,
" "
)
)
;
std
::
cout
<<
std
::
endl
;
return
0
;
}
Output result:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
100 100 100
Summary
- Unified access: different containers use the same traversal logic
- Decouple containers and algorithms: algorithms depend only on iterators
- Improve code reusability: the same code can apply to multiple data structures
For beginners, it is recommended to prioritize mastering the following:
next / prev / distanceback_inserter- Iterator traversal methods