Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about C++ iterators

Tags:

c++

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 behind this?

Also, why can't we cout iterators' content? such as "cout << itr << endl;" wouldn't work. What is the reason behind this? Iterators are a lot like pointers, but we can cout pointers but not iterators, why?

In general, what is the intrinsic difference between iterators and pointers? I used to think they are similar, but I guess I have to understand this in order to get to the next level of understanding c++.

Question#2: Thank you all for awesome answers. I have one more question in regards to iterators. Why c++ prints out something like "50397953" when the iterator goes out of bounds? Shouldn't it be printing something like NULL or '\0' ?

like image 720
Thenewstockton Avatar asked Jul 01 '16 00:07

Thenewstockton


People also ask

Are iterators faster than indexing?

Depending on the actual container, incrementing an iterator might be faster than indexing (think linked lists).

Why use iterators instead of pointers?

A pointer of type T* can point to any type T object. An iterator is more restricted, e.g., a vector::iterator can only refer to doubles that are inside a vector container.

Can iterators be compared?

To compare the values that two iterators are pointing at, dereference the iterators first, and then use a comparison operator. Operator= -- Assign the iterator to a new position (typically the start or end of the container's elements).

Why iterators are useful?

The primary purpose of an iterator is to allow a user to process every element of a container while isolating the user from the internal structure of the container. This allows the container to store elements in any manner it wishes while allowing the user to treat it as if it were a simple sequence or list.


1 Answers

std::list<T> has bidirectional iterators and it doesn't make any sense to require them to be comparable. It's not even clear how it would be possible to implement std::list<T> in order to be compliant with such a requirement. However, since random access iterators support subtraction, it is obvious that we can compare them.

Why can't we print the value of an iterator? In general, it's up to the author whether to support such an operation. Many iterators are basically just implemented as pointers or wrappers around pointers---providing operator<< for them wouldn't give the user any benefit they couldn't get by simply printing out the address of the object pointed to.

As for the difference between iterators and pointers, pointers to objects are a specific kind of random access iterator, but iterators can also be of class type so long as they satisfy the iterator requirements.

like image 170
Brian Bi Avatar answered Sep 18 '22 00:09

Brian Bi