std::vector::data(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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* and const 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