end()

M

Michael

Hi,

I am confused about end() in the following code. end() does not point
to the 10th element? Why? Does end() means like '\0' in a char x [ ]?

Thanks in advance,
Michael

#include <iostream>
#include <vector>

int main()
{
std::vector<int> intVector(10, 1);
int x = 0;

std::vector<int>::iterator iter;
for (iter = intVector.begin();
iter != intVector.end(); iter++)
{
std::cout << "Element #" << x++ << ": "
<< *iter << std::endl;
}

return 0;
}
 
K

Kai-Uwe Bux

Michael said:
Hi,

I am confused about end() in the following code. end() does not point
to the 10th element?

end() does not point to any element. It point *past* the last element.

By convention, all ranges in the standard library are specified either by
start end length or by to iterators, one pointing to the first element and
one pointing *past* the last element. This is, ranges are right-open
intervals [from,to). One reason is that this convention allows one to
specity an empty range by [from,from).
Does end() means like '\0' in a char x [ ]?
No.


Thanks in advance,
Michael

#include <iostream>
#include <vector>

int main()
{
std::vector<int> intVector(10, 1);
int x = 0;

std::vector<int>::iterator iter;
for (iter = intVector.begin();
iter != intVector.end(); iter++)
{
std::cout << "Element #" << x++ << ": "
<< *iter << std::endl;
}

return 0;
}


Best

Kai-Uwe Bux
 
D

Daniel T.

"Michael said:
Hi,

I am confused about end() in the following code. end() does not point
to the 10th element? Why?

End points to one beyond the last element. It does so because that way
when you do the for loop like you did, it will process the last element.
 
J

Jerry Coffin

Hi,

I am confused about end() in the following code. end() does not point
to the 10th element? Why?

Because if it did, iterators would be substantially more difficult to
use.

end() gives an iterator that's basically one past the end of the data
-- i.e. it signals when you've reached a point that there's no more
data. Typical usage is something like:

for (it=x.begin(); it!=x.end(); ++it)

If end() actually "pointed" at the last element, a loop to deal with
an entire collection would be more difficult correctly. In
particular, with some types of collections, ordered comparisons (e.g.
<=) are not guaranteed to work correctly.
 
H

Howard

Kai-Uwe Bux said:
Michael wrote:
Does end() means like '\0' in a char x [ ]?

No.

Well, it does, in a way. Michael asked if it was "like" the null
terminator. In a sense, it is, in that the null terminator in a C-style
string is an element which is one past the end of the actual string, while
end() returns an iterator to one past the last element in the container.
The two differ in that the null terminator is a value in the container,
while end() is a function which returns an iterator. But conceptually, they
are similar in that they are each a way to indicate the "one-past-the-end"
idea.

-Howard
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top