In C++, std::vector::end() returns an iterator that points to the place after the last element. It is an end marker, not a real element, so you must never read through it.
It is the partner of begin(): the pair [begin, end) describes the whole vector. Include <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
Syntax
Call end with empty brackets. It takes no arguments.
C++
auto stop = v.end();
v.insert(v.end(), 4); // add at the back
Call
What it does
v.end()
Returns an iterator one place after the last element.
v.insert(v.end(), 4)
Uses the end marker as the insert position, so 4 goes to the back.
If you only want to read, use cend(). To get the last element itself, use back().
end() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Iterator
A random-access iterator that points past the last element. On a const vector it is a const_iterator.
Empty vector
For an empty vector, end() equals begin().
C++ Examples
Example 1 — Loop Until end()
The loop runs while the iterator is not equal to end().
C++
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {2, 4, 6};
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
return 0;
}
Output
2 4 6
How It Works
it starts at begin(), the element 2.
After 6, ++it makes the iterator equal to end().
The loop condition it != d.end() is now false, so the loop stops before reading anything invalid.
Example 2 — end() as an Insert Position
Passing end() to insert() adds the value at the back.
C++
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {1, 2, 3};
v.insert(v.end(), 4);
for (int n : v) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 2 3 4
How It Works
insert(pos, value) puts the new value beforepos.
Before end() means after the last real element, so 4 goes to the back.
This gives the same result as v.push_back(4).
Example 3 — Compare find() Results with end()
Algorithms use end() to say nothing found.
C++
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {4, 8, 15};
auto a = std::find(v.begin(), v.end(), 16);
auto b = std::find(v.begin(), v.end(), 8);
if (a == v.end()) {
std::cout << "16 not found\n";
}
if (b != v.end()) {
std::cout << "8 found at index " << (b - v.begin()) << '\n';
}
return 0;
}
Output
16 not found
8 found at index 1
How It Works
std::find returns an iterator to the match, or end() if there is no match.
16 is not in the vector, so a == d.end() and the first message prints.
8 is found, so b points to it. b - d.begin() turns the iterator into the index 1.
Frequently Asked Questions
It returns an iterator that points one place after the last element of the vector. It is an end marker, not a real element.
No. It does not point to an element. Reading *d.end() is undefined behavior.
back() returns the last element itself. end() returns an iterator that points after it. The last element is *(d.end() - 1), which is the same as d.back().
Yes. For an empty vector, begin() == end(), so a loop between them runs zero times.
A reallocation from push_back() invalidates all iterators, including end(). Call end() again after changing the vector.