Questions about C ++ Iterators

In C ++, why can't we use '>' or '<' to compare InputIterators, ForwardIterators and BidirectionalIterators? However, we can compare RandomAccessIterators with '>' or '<', for example std :: vector. What is the reason for this?

Also, why can't we use cout iterator content? such as "cout <lt; lt; lt; endl;" will not work. What is the reason for this? Iterators are very similar to pointers, but we can specify cout pointers, but not iterators, why?

In general, what is the internal difference between iterators and pointers? I used to think they were similar, but I think I need to understand this in order to go to the next level of understanding C ++.

Question No. 2: Thank you all for the amazing answers. I have one more question regarding iterators. Why does C ++ produce something like "50397953" when an iterator goes beyond? Shouldn't it print something like NULL or '\ 0'?

+6
source share
3 answers

std::list<T> has bidirectional iterators, and there is no point in requiring comparability from them. It is not even clear how std::list<T> can be implemented to meet this requirement. However, since random access iterators support subtraction, it is obvious that we can compare them.

Why can't we print the value of the iterator? In general, it is up to the author whether such an operation should be supported. Many iterators are basically just implemented as pointers or wrappers around pointers --- providing operator<< for them will not give the user any benefit that they cannot get by simply printing out the address of the object that it points to.

Regarding the difference between iterators and pointers, pointers to objects are a specific type of random access iterator, but iterators can also be class types if they satisfy the requirements of the iterator.

+5
source

There is both a tough, formal explanation, and a practical one.

Tough, official explanation

Since it is indicated in C ++.

Practical explanation

A vector is always stored in contiguous memory by definition. An iterator is, more or less, a pointer. Therefore, by comparing the actual base memory addresses, you can determine which pointer is β€œless” or β€œmore” than another pointer.

On the other hand, each item in a list or set can be stored anywhere. Each item is created independently. An iterator for one particular element in the list may refer to a numerically smaller memory cell than another iterator for another element in the same list, but the actual element may be after another element in the actual list. Given the two iterators in the list, you cannot immediately determine which one is closer to the beginning or end of the list. Note that inserting and deleting items in a list does not invalidate existing list iterators, since each item in the list is created independently.

The practical reason you cannot use iterators from operator<< to std::ostream is because, of course, this overload is undefined. Please note that you can always do the following:

 cout << &*itr << endl; 

This will format the actual memory address of the item. But still this is a dubious use. This is basically pointless.

+3
source

I will not answer the first part of your question, because I agree with what has been said, but I will answer you in two other parts, adding some details. I start with the simplest part to explain.

In general, what is the internal difference between iterators and pointers? I used to think they were similar, but I think I need to understand this in order to go to the next level of understanding C ++.

Iterators are a generalization of pointers.

You can also see that iterator implementation on github

Also, why can't we use cout iterator content? such as "cout <lt; lt; lt; endl;" will not work. What is the reason for this? Iterators are very similar to pointers, but we can specify cout pointers, but not iterators, why?

So what happens inside, as I understand it, is that an iterator is a class that has a field in which a pointer to an element is stored. The '*' operator is overwritten and returns the value that is stored at the address of the pointers.

+1
source

All Articles