iterator beyond the first element

N

Neelesh Bodas

C++ guarantees that there exists a valid iterator beyond the last
element of a container. However, such iterator might not be written or
dereferenced. This is also true for arrays - C++ gurantees that there
exists one position available beyond the end of array.

Does it give similar guarantee about the position before the first
element - the position indicated by the iterator rbegin()? (For arrays
as well as standard containers)
 
V

Victor Bazarov

Neelesh said:
C++ guarantees that there exists a valid iterator beyond the last
element of a container. However, such iterator might not be written or
dereferenced. This is also true for arrays - C++ gurantees that there
exists one position available beyond the end of array.

Does it give similar guarantee about the position before the first
element - the position indicated by the iterator rbegin()? (For arrays
as well as standard containers)

You are mistaken about at least one thing: 'rbegin()' returns an iterator
that points to the _last_ element, not first.

No, C++ does not guarantee the existence of "one-before-first" iterator
(in your terms it would be '--begin()').

V
 
P

Pete Becker

Neelesh said:
Does it give similar guarantee about the position before the first
element
No.

the position indicated by the iterator rbegin()?

rbegin() is completely different. Reverse iterators for containers use
the end() and begin() iterators and index one off from the iterator's
position.
 
S

Sumit Rajan

Neelesh Bodas said:
C++ guarantees that there exists a valid iterator beyond the last
element of a container. However, such iterator might not be written or
dereferenced. This is also true for arrays - C++ gurantees that there
exists one position available beyond the end of array.

Does it give similar guarantee about the position before the first
element - the position indicated by the iterator rbegin()? (For arrays
as well as standard containers)


If you have a copy of "The C++ Standard Library" close to you, please take a
look at Section 7.4.1. You will find a detailed explanation on this.


Regards,
Sumit.
 
N

Neelesh Bodas

Pete said:
rbegin() is completely different. Reverse iterators for containers use
the end() and begin() iterators and index one off from the iterator's
position.

Yeah, that was a typo. And yes, I get what you are saying - you mean to
say that the logical position and physical position of reverse
iterators are different - they are one unit off - Is that right? If so,
My confusion is resolved.
 
N

Neelesh Bodas

Sumit said:
If you have a copy of "The C++ Standard Library" close to you, please take a
look at Section 7.4.1. You will find a detailed explanation on this.

Thanks Sumit, Just opened Josuttis' Book. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top