std::array::data(3)
π 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
- 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