std::array::data(3)

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

πŸŽ€ std::array::data(3) πŸŽ€

okay bestie, this is from the C++ Standard Libary manual (manual name folded in right at the top, as requested, periodt). it's giving β€œpointer to the actual storage” and honestly she's a little iconic for it πŸ’…


✨ NAME ✨

std::array::data - std::array::data

(no cap, the name really says it twice β€” she's like that 🫢)


πŸ¦‹ Synopsis πŸ¦‹

T* data() noexcept;             (1) (since C++11)
                                    (constexpr since C++17)
const T* data() const noexcept; (2) (since C++11)
                                    (constexpr since C++14)

Returns pointer to the underlying array serving as element storage. The pointer is such that range [data(), data() + size()) is always a valid range, even if the container is empty (data() is not dereferenceable in that case).


🌸 Parameters 🌸

(none) β€” literally zero parameters, she takes no arguments, main character energy only ✨


πŸ’– Return value πŸ’–

Pointer to the underlying element storage. For non-empty containers, the returned pointer compares equal to the address of the first element. slay.


πŸ”₯ Complexity πŸ”₯

Constant. no cap, it's constant.


πŸ¦‹ Notes πŸ¦‹

If size() is 0, data() may or may not return a null pointer. iykyk β€” empty-range behavior is just like that sometimes.


🌈 Example 🌈

// Run this code β€” yes bestie, actually run it πŸ‘‘

        #include <cstddef>
        #include <iostream>
        #include <span>
        #include <array>

        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::array<int, 4> 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()});
        }

🌟 Output 🌟

        data = 1 2 3 4
        data = 1 2 3 4

πŸ’œ See also πŸ’œ

  • front β€” access the first element (public member function), the opening act bestie πŸŽ€
  • back β€” access the last element (public member function), the finale, the closer, the last slice of strawberry cake πŸ“
  • size β€” returns the number of elements (public member function), the headcount queen πŸ‘‘
  • span β€” a non-owning view over a contiguous sequence of objects (C++20) (class template), the safe little window vibe πŸͺŸβœ¨
  • data β€” obtains the pointer to the underlying array (C++17) (function template), another data getter serving looks πŸ’…

shoutout to http://cppreference.com for the source, and this page is stamped with version/date 2024.06.10 β€” mwah, bye bestie πŸ’‹πŸ«Ά

πŸ“¦ where this page comes from