C++ std::vector begin() Function

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

Definition

In C++, std::vector::begin() returns an iterator that points to the first element of the vector. An iterator works like a pointer: *it reads the element it points to.

You usually use it together with end() in a loop, or pass the pair to algorithms such as std::sort. 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 begin with empty brackets. It takes no arguments.

C++
auto it = v.begin();
*it = 100;
CallWhat it does
v.begin()Returns an iterator to the first element.
*v.begin()Reads the first element through the iterator. You can also assign to it.

If you only want to read, use cbegin(). It always gives a read-only iterator.

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

Parameters

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

Return Value

Return ValueDescription
IteratorA random-access iterator to the first element. On a const vector it is a const_iterator.
Empty vectorFor an empty vector, begin() is equal to end(), and you must not dereference it.

C++ Examples

Example 1 — Loop with begin()

Walk through every element with an iterator.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {5, 10, 15, 20};

    for (auto it = v.begin(); it != v.end(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. auto it = d.begin() starts the iterator on the first element.
  2. *it reads the current value, and ++it moves to the next one.
  3. The loop stops when it reaches v.end().

Example 2 — Change Values Through the Iterator

A non-const iterator lets you write to the element. A vector iterator also supports +.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {1, 20, 30};

    auto it = v.begin();
    *it = 100;
    *(it + 1) += 5;

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. *it = 100 replaces the first element, which was 1.
  2. it + 1 jumps straight to the second element. Vector iterators are random-access, so this is allowed.
  3. += 5 adds 5 to 20, so it becomes 25. The range-based loop prints the final values.

Example 3 — Sort with begin() and end()

Algorithms take a pair of iterators. Here std::sort sorts the whole vector.

C++
#include <algorithm>
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {9, 2, 7, 4, 5};

    std::sort(v.begin(), v.end());

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::sort needs to know where the range starts and where it stops.
  2. v.begin() is the start, and v.end() is the stop mark. A vector has random-access iterators, so std::sort works on it (a std::list cannot use it).
  3. The values inside the same vector are rearranged in place, so it now reads 2 4 5 7 9.

Frequently Asked Questions

It returns an iterator that points to the first element of the vector. For a const vector it returns a const_iterator.
front() returns the first element itself. begin() returns an iterator that points to it. You write *d.begin() to get the same value as d.front().
Yes. It returns a valid iterator that equals end(). You must not dereference it, because there is no element.
push_back() may reallocate and invalidate all iterators, references, and pointers. The simple rule: get a new iterator after you add or remove elements, or call reserve() first.
For a plain loop over every element, a range-based for loop is simpler. Use begin() when you need an algorithm such as std::sort, or when you need to move the iterator yourself.

Next: capacity()

How many elements fit before the next reallocation with std::vector.

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

12 people found this page helpful