C++ <list> - end() Function
The C++ list::end function returns the iterator pointing to the past-the-last element of the list container. The past-the-last element of the list is the theoretical element that follows the last element. It does not point to any element, and hence could not be dereferenced.
Syntax
iterator end(); const_iterator end() const;
iterator end() noexcept; const_iterator end() const noexcept;
Parameters
No parameter is required.
Return Value
An iterator to the past-the-last element of the sequence container. If the sequence object is constant qualified, the function returns a const_iterator, else returns an iterator.
Time Complexity
Constant i.e, Θ(1).
Example:
In the example below, the list::end function returns the iterator pointing to the past-the-last element of the list MyList.
#include <iostream> #include <list> using namespace std; int main (){ list<string> MyList{"Alpha","Coding","Skills"}; list<string>::iterator it; it = MyList.end(); it--; cout<<*it<<" "; it--; cout<<*it<<" "; it--; cout<<*it<<" "; return 0; }
The output of the above code will be:
Skills Coding Alpha
Example:
Lets see another example where the list called MyList contains integer values and list::end function is used with list::begin function to specify a range including all elements of the list container.
#include <iostream> #include <list> using namespace std; int main (){ list<int> MyList{10, 20, 30, 40, 50}; list<int>::iterator it; for(it = MyList.begin(); it != MyList.end(); ++it) cout<<*it<<" "; return 0; }
The output of the above code will be:
10 20 30 40 50
❮ C++ <list> Library