C++ std::vector end() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::vector

Definition

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
CallWhat 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().

Library declarations
C++
iterator       end() noexcept;
const_iterator end() const noexcept;

Parameters

ParameterDescription
Noneend() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
IteratorA random-access iterator that points past the last element. On a const vector it is a const_iterator.
Empty vectorFor 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;
}

How It Works

  1. it starts at begin(), the element 2.
  2. After 6, ++it makes the iterator equal to end().
  3. 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;
}

How It Works

  1. insert(pos, value) puts the new value before pos.
  2. Before end() means after the last real element, so 4 goes to the back.
  3. 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;
}

How It Works

  1. std::find returns an iterator to the match, or end() if there is no match.
  2. 16 is not in the vector, so a == d.end() and the first message prints.
  3. 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.

Next: erase()

Remove one element or a range of elements with std::vector.

erase() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful