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;
Call
What 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.
begin() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Iterator
A random-access iterator to the first element. On a const vector it is a const_iterator.
Empty vector
For 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;
}
Output
5 10 15 20
How It Works
auto it = d.begin() starts the iterator on the first element.
*it reads the current value, and ++it moves to the next one.
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;
}
Output
100 25 30
How It Works
*it = 100 replaces the first element, which was 1.
it + 1 jumps straight to the second element. Vector iterators are random-access, so this is allowed.
+= 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;
}
Output
2 4 5 7 9
How It Works
std::sort needs to know where the range starts and where it stops.
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).
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.