What is the difference between an iterator and a pointer in a C++ language?
c++ language-specification
The C++ standard library (the STL underlying the ) consists of three components: Container, iterator, and Algorithm.
In the absence of an iterator, various algorithms such as copy()
, sort()
, and find()
must be prepared for each container type (vector
, deque
, list
...).In other words, N algorithms x M containers require (N x M) implementation functions.
In fact, the algorithm does not work directly on the container itself, but on the "pointer-like" of the container.This "pointer pointing to the container element" is the "Iterator.The characteristics of the iterator are determined by the container's capabilities.By providing algorithms for each iterator, rather than for each container, you can fit into N types of algorithms + m types of iterators, or (N+m) implementation functions.
The iterator is a class or template implementation of a pointer-like interface.
So the real thing may be a pointer or not.
The iterator can be used like a pointer, but
There are five types of iterators, and unlike pointers, ++
, --
, *
, []
can be used like pointers or compared to each other (what can be done depends on the type).
© 2024 OneMinuteCode. All rights reserved.