In C++, std::list::end() returns an iterator that points one place after the last element of a std::list. It does not point at a real element.
It works as a stop sign. Loops run until the iterator reaches end(), and search functions return end() when they find nothing. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call end with empty brackets. Compare iterators with it, but do not dereference it.
C++
auto stop = l.end();
bool done = (it == l.end());
l.insert(l.end(), 4); // add at the end
int last = *std::prev(l.end()); // last element
Call
What it does
l.end()
Returns an iterator to the position after the last element. For a const list it returns a const_iterator.
Because a list is doubly linked, std::prev(l.end()) reaches the last element quickly. l.back() does the same and is shorter.
end() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
iterator
An iterator to the position after the last element. It must not be dereferenced.
C++ Examples
Example 1 — end() Means “Not Found”
Search functions return end() when they find nothing. Compare the result with l.end().
C++
#include <algorithm>
#include <iostream>
#include <list>
int main() {
std::list<int> l = {10, 20, 30};
auto a = std::find(l.begin(), l.end(), 20);
auto b = std::find(l.begin(), l.end(), 99);
std::cout << (a != l.end() ? "found 20" : "no 20") << '\n';
std::cout << (b != l.end() ? "found 99" : "no 99") << '\n';
return 0;
}
Output
found 20
no 99
How It Works
std::find() walks from begin() up to, but not including, end(), looking for the value.
For 20 it stops at a real node, so the iterator a is not equal to l.end().
For 99 it walks all the way to the end and returns l.end(), so the test b != l.end() is false.
Example 2 — end() with std::prev()
Step one place back from end() to land on the last element.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {10, 20, 30};
auto last = std::prev(l.end());
*last = 99;
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
10 20 99
How It Works
l.end() points after the last node, so we cannot read it. std::prev(l.end()) moves one step back.
The result last points at the node with 30. *last = 99 writes into that node.
The loop prints 10 20 99. This does the same as l.back() = 99.
Example 3 — end() as an Insert Position
Inserting before end() adds a new last element. The same end() iterator stays valid.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> l = {1, 2, 3};
auto stop = l.end();
l.insert(l.end(), 4);
std::cout << std::boolalpha;
std::cout << "end unchanged: " << (stop == l.end()) << '\n';
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
end unchanged: true
1 2 3 4
How It Works
stop remembers the end() iterator before we change the list.
insert(l.end(), 4) puts a new node beforeend(), so 4 becomes the last element.
For a list, the end position does not move when you insert, so stop == l.end() is true. The output shows 1 2 3 4.
Frequently Asked Questions
An iterator that points one place after the last element. It is not a real element, so you must not dereference it.
It lets C++ use half-open ranges [begin, end). An empty list is then simply begin() == end(), and loops need only one test: it != end().
Use std::prev(l.end()) or --l.end(). It is shorter to call l.back(), which returns a reference to the last element.
The behavior is undefined. The program may crash or show garbage.
For std::list, an end() iterator that you stored stays equal to l.end() after insert(). Still, it is a good habit to call l.end() again in loops that change the list.