Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an input iterator be dereferenced only on the right-hand sign of an assignment?

Tags:

c++

iterator

I have this simple question: in C++ primer 5ed by Lippman chapter 10. "The iterator categories" it is said:

The Iterator Categories

Input iterators: can read elements in a sequence. An input iterator must provide

  • Equality and inequality operators (==, !=) to compare two iterators

  • Prefix and postfix increment (++) to advance the iterator

  • Dereference operator (*) to read an element; dereference may appear only on the right-hand side of an assignment

  • The arrow operator (->) as a synonym for (*it).member—that is, dereference the iterator and fetch a member from the underlying object.

Did he mean with "Dereference operator (*) to read an element; dereference may appear only on the right-hand side of an assignment" : on the "left side of an assignment".??

I am confused about it. Thank you.

like image 779
Itachi Uchiwa Avatar asked Sep 26 '19 11:09

Itachi Uchiwa


Video Answer


1 Answers

The author meant that if you have an input iterator it, then you can do:

foo = *it;

but it's possible you may not be able to do:

*it = foo;

I.e. you can read from an iterator (it's on the right side of an assignment), but there is no guarantee you can write to it (it's on the left).

like image 191
StoryTeller - Unslander Monica Avatar answered Nov 13 '22 00:11

StoryTeller - Unslander Monica