CKTIS / C++
std::vector::end — past-the-end iterator
`std::vector::end` returns the position just past the last element, not the last element itself. Use it as an exclusive range boundary and to detect a failed search.
Version: ISO C++23; implementation support may vary
Syntax
#include <vector>
// Member declarations of std::vector<T, Allocator> in C++23:
constexpr iterator end() noexcept;
constexpr const_iterator end() const noexcept;
// Related member for read-only iteration:
constexpr const_iterator cend() const noexcept;std::vector::end is a member function provided by <vector>. Calling v.end() takes constant time and returns an iterator for a non-const vector or a const_iterator for a const vector. For read-only iteration even on a non-const vector, pair v.cbegin() with v.cend(). Both end() overloads have been noexcept since C++11; cend() was introduced in C++11 and is also noexcept. All three declarations have been constexpr since C++20. For a vector, the non-member std::end(v) calls the member.
The range [v.begin(), v.end()) includes every element but excludes the endpoint. An empty vector has v.begin() == v.end(). Do not dereference or increment the endpoint: it denotes no element, and *v.end() has undefined behavior. If you need the last element itself, use v.back() after checking !v.empty(). Do not assume the iterator is a raw pointer.
std::find_if(first, last, pred) returns the first iterator whose element satisfies the predicate, or last if no element does. Compare the result with that same, still-valid endpoint before dereferencing it. Take both endpoints from the same vector.
A successful push_back() invalidates the old endpoint even without reallocation; in that case, iterators to existing elements remain valid. reserve() does not change the size, but if it reallocates, it invalidates every iterator, including the endpoint, as well as pointers and references to elements. After swap(), do not rely on a saved endpoint remaining valid, even though element iterators stay valid. Reacquire the iterators you need after an invalidating operation, and do not use them after the vector is destroyed.
Examples
#include <algorithm>
#include <iostream>
#include <vector>
void report_first_at_least(const std::vector<int>& values, int threshold)
{
const auto stop = values.end();
const auto found = std::find_if(values.begin(), stop,
[threshold](int value) { return value >= threshold; });
if (found != stop) {
std::cout << *found << '\n';
} else {
std::cout << "not found\n";
}
}
int main()
{
std::vector<int> values{4, 7, 9};
values.reserve(8);
report_first_at_least(values, 10);
values.push_back(12);
report_first_at_least(values, 10);
}Result
not found 12
The first search prints `not found`. `reserve(8)` leaves room to append `12` without reallocating, but the append still invalidates the old endpoint. The second call obtains a fresh endpoint and prints `12`.
Related lessons
- C++ · Lesson 10 · Advanced · Version: 3
- C++ · Lesson 11 · Advanced · Version: 3
- C++ · Lesson 14 · Advanced · Version: 3
- C++ · Lesson 15 · Advanced · Version: 3
- C++ · Lesson 23 · Advanced · Version: 3
- C++ · Lesson 29 · Advanced · Version: 3
- C++ · Lesson 30 · Advanced · Version: 3
- C++ · Lesson 32 · Advanced · Version: 3
- C++ · Lesson 34 · Advanced · Version: 3
- C++ · Lesson 35 · Advanced · Version: 3
- C++ · Lesson 40 · Advanced · Version: 3
- C++ · Lesson 45 · Advanced · Version: 3
- C++ · Lesson 57 · Advanced · Version: 3
Sources
- std::vector<T,Allocator>::end, std::vector<T,Allocator>::cend
- N4950 — Working Draft, Standard for Programming Language C++
- Class template vector — Overview [vector.overview]
- General containers [container.reqmts]
- Iterator requirements — General [iterator.requirements.general]
- Class template vector — Capacity [vector.capacity]
- Class template vector — Modifiers [vector.modifiers]
- Find [alg.find]
- std::end, std::cend