Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::vector::begin — start of a vector range

`begin()` returns the vector's starting iterator. The vector's constness selects the overload, and `end()` marks the position you must not dereference.

Version: ISO C++23; implementation support may vary

Syntax

// Public member declarations of std::vector<T, Allocator> in <vector> (ISO C++23)
constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;

std::vector::begin is a member function provided by <vector>. It takes constant time, O(1), and returns an iterator to the first element when the vector is non-empty. Both overloads have been noexcept since C++11 and constexpr since C++20; in C++23, constant evaluation still depends on the context and implementation support.

For a non-const vector, v.begin() returns an iterator that lets you modify elements. For a vector accessed through const, it returns a const_iterator, which does not let you modify elements through it. Use v.cbegin() (since C++11) to get a const_iterator even from a mutable vector. Do not assume an iterator is a raw pointer or that *it gives T& for every T: a writable std::vector<bool> iterator uses a proxy.

The half-open range [v.begin(), v.end()) covers the elements of one vector and excludes the past-the-end position. For an empty vector, v.begin() == v.end(), and dereferencing either iterator is undefined behavior. std::find_if returns the supplied range endpoint when nothing matches; test the result before using *it.

Reallocation invalidates iterators, pointers and references to elements, as well as a saved end(). reserve() adds no elements but may reallocate. A push_back() without reallocation preserves iterators to existing elements but invalidates the old end(). Inserting at the front or erasing the first element can invalidate a saved begin() even without reallocation. Get fresh iterators after invalidation. Do not use an iterator from a temporary vector beyond the full expression that created the vector.

Examples

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> values{2, 7, 4};
    if (!values.empty()) {
        *values.begin() = 3;
    }

    const auto& read = values;
    const int threshold = 5;
    const auto last = read.end();
    const auto match = std::find_if(read.begin(), last, [threshold](int value) {
        return value >= threshold;
    });

    if (read.cbegin() != read.cend()) {
        std::cout << *read.cbegin() << '\n';
    }
    if (match != last) {
        std::cout << *match << '\n';
    }

    const std::vector<int> empty;
    if (empty.begin() == empty.end()) {
        std::cout << "empty\n";
    }
}

Result

3
7
empty

`*values.begin() = 3` changes the first element. The search through a const reference uses a threshold captured by value and reads the match `7` only after checking the endpoint. The empty vector's iterators are compared without dereferencing them.

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