C++ std::vector data() Function

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

Definition

In C++, std::vector::data() returns a pointer to the memory where the vector keeps its elements. Because a vector is contiguous, that pointer behaves like a plain C array of size() elements.

It is the safe modern form of &v[0]. Prefer data(), especially when the vector might be empty. 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 data with empty brackets.

C++
int* p = v.data();
const int* q = cv.data();
legacy_api(v.data(), v.size());
CallWhat it does
v.data()Returns a T* to the first element (or a non-dereferenceable pointer if empty).
v.data()[i]Same as v[i] when the vector is not empty and i is in range.

For an empty vector, data() may return nullptr or another non-dereferenceable pointer. Do not read or write through it. Always check empty() or pass size() to the other API.

Library declarations
C++
T* data() noexcept;
const T* data() const noexcept;

Parameters

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

Return Value

Return ValueDescription
T* / const T*Pointer to the first element, or a non-dereferenceable pointer when size() is 0.

C++ Examples

Example 1 — Basic data()

Get a pointer to the first element and print a few values through it.

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

int main() {
    std::vector<int> v = {10, 20, 30};
    int* p = v.data();

    std::cout << p[0] << ' ' << p[1] << ' ' << p[2] << '\n';
    return 0;
}

How It Works

  1. v.data() returns the address of the first element, 10.
  2. Because storage is contiguous, p[1] and p[2] are the next elements.
  3. This matches how a C array works, which is why data() is useful with older APIs.

Example 2 — Pass data() to a C-style Function

A helper takes a pointer and a count. Vector supplies both.

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

int sum(const int* p, std::size_t n) {
    int total = 0;
    for (std::size_t i = 0; i < n; ++i) {
        total += p[i];
    }
    return total;
}

int main() {
    std::vector<int> v = {1, 2, 3, 4};
    std::cout << "sum=" << sum(v.data(), v.size()) << '\n';
    return 0;
}

How It Works

  1. The function expects a C array pointer and a length.
  2. v.data() is the pointer and v.size() is the length.
  3. Together they describe the whole contiguous block without copying.

Example 3 — Do Not Dereference data() When Empty

Guard with empty() before reading through the pointer.

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

void show_first(const std::vector<int>& v) {
    if (v.empty()) {
        std::cout << "empty\n";
        return;
    }
    std::cout << "first=" << *v.data() << '\n';
}

int main() {
    std::vector<int> a;
    std::vector<int> b = {42};
    show_first(a);
    show_first(b);
    return 0;
}

How It Works

  1. For the empty vector, the function returns early. It never dereferences data().
  2. For {42}, data() points at the only element, so printing *v.data() is safe.
  3. std::array is never empty in the same way; a vector can shrink to size 0, so this check matters.

Frequently Asked Questions

A pointer to the first element of the contiguous storage, or a non-dereferenceable pointer if the vector is empty.
Use it for C APIs that want a T* and a length. Prefer at(), [], or iterators in normal C++ code.
When the vector is not empty, yes they point to the same place. data() is clearer and defined for empty vectors (but still must not be dereferenced).
No. data() returns a raw pointer. begin() returns an iterator. They often address the same element, but the types differ.
std::deque and std::list are not one contiguous block for all elements, so they have no data(). std::array and std::vector do.

Next: emplace()

Build a new element in place at any position (C++11) with std::vector.

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

10 people found this page helpful