std::vector::data(3)
π std::vector::data(3) β the C++ Standard Library's most "just give me the pointer already" moment
okay so bestie, you're reading the C++ Standard Library manual page for std::vector::data, and honestly? it's a short one, we love that for us. let's get into it β¨
πΈ NAME πΈ
std::vector::data β yes. that's it. that's the name. iconic minimalism, no notes. π
π Synopsis π
T* data(); (1) (noexcept since C++11)
(constexpr since C++20)
const T* data() const; (2) (noexcept since C++11)
(constexpr since C++20)
so there's two flavors, bestie β overload (1) gives you a T*, overload (2) is the const one giving you a const T*. both are noexcept since C++11 (slay) and constexpr since C++20 (double slay).
what does it actually do? it returns a pointer to the underlying array serving as element storage. and here's the part that's giving "trust me bro, it's valid" β the pointer is such that the range [data(), data() + size()) is always a valid range, even if the container is empty. π«Ά
β οΈ but listen: data() is not dereferenceable in that empty case. so like. valid range β valid to poke at. do not poke. π
π¦ Parameters π¦
none. zero. nada. (none). it takes no arguments and it's thriving. β¨
β Return value β
a pointer to the underlying element storage π
and for non-empty containers, that returned pointer compares equal to the address of the first element. so like, it is &container[0], but we don't say that out loud (more on that in a sec π).
π Complexity π
Constant. O(1) baby, it's literally just handing you a pointer. no cap. π₯
π Notes π
here's the chaotic bit: if size() is 0, data() may or may not return a null pointer. yes. either. it's giving "unspecified, deal with it". so don't build your whole personality on assuming it's null, k? π
π Example π
run this code, it's cute:
#include <cstddef>
#include <iostream>
#include <span>
#include <vector>
void pointer_func(const int* p, std::size_t size)
{
std::cout << "data = ";
for (std::size_t i = 0; i < size; ++i)
std::cout << p[i] << ' ';
std::cout << '\n';
}
void span_func(std::span<const int> data) // since C++20
{
std::cout << "data = ";
for (const int e : data)
std::cout << e << ' ';
std::cout << '\n';
}
int main()
{
std::vector<int> container{1, 2, 3, 4};
// Prefer container.data() over &container[0]
pointer_func(container.data(), container.size());
// std::span (C++20) is a safer alternative to separated pointer/size.
span_func({container.data(), container.size()});
}
and the output is:
data = 1 2 3 4
data = 1 2 3 4
see what happened there? pointer_func and span_func said the exact same thing, but span_func is the safer one and honestly it ate. π
π§ Defect reports π§
these are the behavior-changing defect reports that got applied retroactively to previously published C++ standards. so like, glow-ups that happened after the fact. π
- LWG 464 β applied to C++98. behavior as published: vector did not have this member function. corrected behavior: added. (not them forgetting the whole member function π)
- LWG 1312 β applied to C++98. behavior as published: the return type were pointer and const_pointer. corrected behavior: changed to
T*andconst T*respectively. (glow-up fr fr β¨)
π See also π
frontβ access the first element (public member function)backβ access the last element (public member function)sizeβ returns the number of elements (public member function)span(C++20) β a non-owning view over a contiguous sequence of objects (class template)data(C++17) β obtains the pointer to the underlying array (function template)
okay that's the whole page, bestie, you're basically a vector internals expert now π«Άπ
source/version & date, since you asked so nicely: this one came from http://cppreference.com, dated 2024.06.10. mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms