Vastor
Vastor

Reputation: 77

where does v.end() point to?

for (vector<Student_info>::const_iterator iter = students.begin();
     iter != students.end(); ++iter)
    cout << (*iter).name << endl;

In Accelerated C++, code above was told to output all of the data contain in the vector, but if the loop stop when iter != students.end() equal to students.end(), then how does it will proceed to the last element of the vector?

Upvotes: 1

Views: 5428

Answers (5)

arjacsoh
arjacsoh

Reputation: 9242

If I understood well what you asked,it is so because the last element of a vector is actually on the position v.end()-1. So to an element before the v.end() should the loop stop to access all the elements. Is that you asked?

Upvotes: 3

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361612

Actually .end() does not return the iterator to the last element, rather it returns iterator to past-the-last-element. The iterator to the last element is just before it; that is, if it points to the last element, then ++it makes it to point to past-the-last-element.

The language specification calls it past-the-end iterator.

§24.1/5 (C++03) reads,

Just as a regular pointer to an array guarantees that there is a pointer value pointing past the last element of the array, so for any iterator type there is an iterator value that points past the last element of a corresponding container. These values are called past-the-end values. Values of an iterator i for which the expression *i is defined are called dereferenceable. The library never assumes that past-the-end values are dereferenceable

Also note that dereferencing the past-the-end iterator invokes undefined behavior:

auto it = students.end(); //ok
it->someFunction();       //undefined behavior
(*it).someFunction();     //undefined behavior

Upvotes: 1

sharptooth
sharptooth

Reputation: 170509

end() returns a special value that means "iteration ended, stop". Usually that's an iterator to the element beyond the array end - you don't have to process it, you should stop once you reached it and that's what the loop does.

Upvotes: 3

Etienne de Martel
Etienne de Martel

Reputation: 36984

end() points to somewhere past the end of the container. By convention, sequences in C++ are of the form [begin, end), that is, the end is not part of the sequence. For arrays and vectors, end() points to the index equal to the size of the array (which is outside the array or vector).

This convenient in many aspects. For an empty sequence, for instance, the begin and end of the container are the same element. Also, the value returned by end() is commonly used as an "invalid" iterator, as it does not point to a meaningful element.

Upvotes: 4

Greg Hewgill
Greg Hewgill

Reputation: 993901

The loop you have written is roughly equivalent to:

vector<Student_info>::const_iterator iter = students.begin();
while (iter != students.end()) {
    cout << (*iter).name << endl;
    ++iter;
}

This is the normal expansion of the for loop into its while equivalent1. Every time through the loop, the iter iterator is incremented (with ++iter) to advance to the next element. When the end of the vector is reached, then iter will equal students.end() and the loop will exit.

1. Except for the scope of iter remains within the body of the for loop.

Upvotes: 3

Related Questions