end

Syntax:

    #include <list>
    iterator end();
    const_iterator end() const;

The end() function returns an iterator just past the end of the list.

Note that before you can access the last element of the list using an iterator that you get from a call to end(), you'll have to decrement the iterator first.

For example, the following code uses begin() and end() to iterate through all of the members of a list:

   list<int> v1( 5, 789 );
   list<int>::iterator it;
   for( it = v1.begin(); it != v1.end(); ++it ) {
     cout << *it << endl;
   }

The iterator is initialized with a call to begin(). After the body of the loop has been executed, the iterator is incremented and tested to see if it is equal to the result of calling end(). Since end() returns an iterator pointing to an element just after the last element of the list, the loop will only stop once all of the elements of the list have been displayed.

end() runs in constant time.

Related Topics: begin, rbegin, rend