Iterator = pointer? Or what is it?

Is an iterator in C ++ a pointer? The reason I ask is because no one seems to understand what an iterator is. It is simply a “thing” or “value” that they say. But the iterator simply points to the element, its position. When we play it, he likes to look at what the iterator points to. Is this the right analogy? Please, help

+4
source share
5 answers

Short answer:

  • A pointer is a kind of iterator.
  • A pointer can therefore be used as an iterator.
  • A pointer has properties other than an iterator.

Story

C-, ++ ++. , .

, 1990- , , " ", ++. " " , STL ( ) , " ". C , vector, deque , , , C- . C, , C- .

, , - , .

C . , , .

- , , ++.

, .

, , . " ". , , .

. , .

+3

. " ".

, . - , .

, ( ). , " ". , . , / ..

+3

, . , , , operator++(), operator++(int), operator--(), operator--(int), operator->(), operator*() .. , d . .

+1

- , . . , , . Forward ++ .

http://www.cplusplus.com/reference/iterator/

, , , . , . std::vector::iterator, , iterator typedef T* iterator

, .

+1

C , for , :

int arr[MAX];

for (int* p = arr; p < arr + MAX; ++p)
{
    do_something_with(*p);
}

, . - , , - .. - , ++.

++ , .

std::set<int> s;

for (std::set<int>::const_iterator it = s.begin(); it != s.end(); ++it)
{
    do_something_with(*it);
}

std::set<T>::const_iterator - , ++ * , . ++ (-) , .

std::vector std::string , , typedefs .

, . , find, .

0

All Articles