Differences between iterators and pointers

Asked 1 years ago, Updated 1 years ago, 62 views

What is the difference between an iterator and a pointer in a C++ language?

c++ language-specification

2022-09-30 17:40

2 Answers

  • "pointer" is a type of type defined as part of the C++ language specification.
  • Iterator refers to any type of that meets the requirements and is defined by the C++ standard library specification.
  • A pointer can be considered a type of iterator.The iterator in an array container is a pointer type.

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.


2022-09-30 17:40

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).


2022-09-30 17:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.