C++ std::vector front() Function

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

Definition

In C++, std::vector::front() returns the first element of a std::vector. It is the same as d[0], but it reads better.

It returns a reference, so you can read the value or change it. The vector must have at least one element. 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 front with empty brackets. It takes no arguments.

C++
v.front();
v.front() = 1;
CallWhat it does
v.front()Reads the first element.
v.front() = 1Replaces the first element with a new value.

The last element has a matching function: back(). A vector has no pop_front(); erase at begin() if you must remove the first element.

Library declarations
C++
reference       front();
const_reference front() const;

Parameters

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

Return Value

Return ValueDescription
ReferenceA reference to the first element, which is the same as d[0] and *d.begin().

C++ Examples

Example 1 — Basic front()

Print the first element of a small vector.

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

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

    std::cout << "First element: " << v.front() << '\n';
    return 0;
}

How It Works

  1. The vector holds three numbers, and 5 was stored first.
  2. front() returns a reference to that first slot.
  3. cout prints the value, so the output is 5.

Example 2 — Change the First Element

Assign to front() to replace the first value.

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

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

    v.front() = 1;

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

How It Works

  1. front() is a reference, so it can sit on the left side of =.
  2. Only the first slot changes from 5 to 1. The rest of the vector stays the same.
  3. The loop prints all elements, so you can see the new first value.

Example 3 — Check empty() Before front()

Never call front() on an empty vector. Guard it first.

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

void show(const std::vector<int>& v) {
    if (v.empty()) {
        std::cout << "empty\n";
    } else {
        std::cout << "front=" << v.front() << '\n';
    }
}

int main() {
    std::vector<int> a;
    std::vector<int> b = {7, 8};
    show(a);
    show(b);
    return 0;
}

How It Works

  1. The helper takes the vector by const reference so it can only read.
  2. For the empty vector, empty() is true, so front() is never called.
  3. For {7, 8} the check fails and front() safely prints 7.

Frequently Asked Questions

It returns a reference to the first element of the vector. That is the element at index 0.
No. Calling front() on an empty vector is undefined behavior. Check empty() first.
front() returns the first element itself. begin() returns an iterator that points to it. *v.begin() gives the same value as v.front().
No. front() only looks at it. To remove the last element use pop_back(). To remove the first element use erase(v.begin()), which is slower on a vector.
Both read the first element when the vector is not empty. at(0) throws if the vector is empty; front() is undefined behavior on an empty vector.

Next: get_allocator()

Get a copy of the allocator the vector uses with std::vector.

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

7 people found this page helpful