std::basic_string::data(3)

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

πŸ’– NAME

std::basic_string::data β€” bestie, this is the one that hands you the raw pointer to your string's actual character storage ✨ (yes, the manual name is C++ Standard Library, and we're deep in std::basic_string territory, iykyk)


✨ Synopsis

const CharT* data() const; (1) (noexcept since C++11)
                               (constexpr since C++20)
CharT* data() noexcept;    (2) (since C++17)
                               (constexpr since C++20)

Okay so what this does: it returns a pointer to the underlying array that serves as the character storage πŸŽ€. And the way the pointer works, the range

[data(), data() + size()) (until C++11)
[data(), data() + size()] (since C++11)

is valid, and the values inside it correspond to the values stored in the string. Periodt.

Now here's the pre-C++11 era lore πŸ™ˆ: the returned array is not required to be null-terminated. And if empty() returns true, the pointer is a non-null pointer that should not be dereferenced β€” literally don't touch it, bestie, it's giving "look but don't touch" energy.

But since C++11? Glow up πŸ’…. The returned array is null-terminated β€” that is, data() and c_str() perform the same function. And if empty() returns true, the pointer points to a single null character. Iconic.

⚠️ Vibe check: the pointer you get from data() may be invalidated by: - Passing a non-const reference to the string to any standard library function, or - Calling non-const member functions on the string, excluding operator[](), at(), front(), back(), begin(), end(), rbegin(), rend().

And the numbered overloads, since they're numbered:

  1. Modifying the character array accessed through the const overload of data has undefined behavior. Not me warning you twice 😭 β€” do not do it.
  2. Modifying the past-the-end null terminator stored at data() + size() to any value other than CharT() has undefined behavior. Again: do not.

🫢 Parameters

(none) β€” she takes nothing, she just delivers πŸ’…


πŸŽ€ Return value

A pointer to the underlying character storage. That's it, that's the whole thing.

And the guarantees, split by era:

  • until C++11: data()[i] == operator[](i) for every i in [0, size()).
  • since C++11: data() + i == std::addressof(operator[](i)) for every i in [0, size()).

πŸ’… Complexity

Constant. O(1) queen, no scrolling, no searching, just vibes 🌈


🌸 Example

// Run this code

#include <algorithm>
#include <cassert>
#include <cstring>
#include <string>

int main()
{
    std::string const s("Emplary");
    assert(s.size() == std::strlen(s.data()));
    assert(std::equal(s.begin(), s.end(), s.data()));
    assert(std::equal(s.data(), s.data() + s.size(), s.begin()));
    assert('\0' == *(s.data() + s.size()));
}

Look at those asserts hitting different 😌 every single one passes, no cap. Note the string is literally "Emplary" β€” yes, spelled like that β€” and yes, '\0' == *(s.data() + s.size()) is the null terminator being its little self πŸŽ€


πŸ¦‹ See also

  • front β€” accesses the first character (public member function) (DR*)
  • back β€” accesses the last character (public member function) (DR*)
  • c_str β€” returns a non-modifiable standard C character array version of the string (public member function)
  • data β€” returns a pointer to the first character of a view (public member function of std::basic_string_view<CharT,Traits>)

πŸ’– that's the whole page bestie, you ate πŸ’–

Source: http://cppreference.com β€” dated 2024.06.10, and this lil page is std::basic_string::data(3). Go forth and dereference responsibly 🫢✨

πŸ“¦ where this page comes from