std::basic_string::data(3)
π 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 fromdata()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, excludingoperator[](),at(),front(),back(),begin(),end(),rbegin(),rend().
And the numbered overloads, since they're numbered:
- Modifying the character array accessed through the const overload of
datahas undefined behavior. Not me warning you twice π β do not do it. - Modifying the past-the-end null terminator stored at
data() + size()to any value other thanCharT()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 everyiin[0, size()). - since C++11:
data() + i == std::addressof(operator[](i))for everyiin[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 ofstd::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
- 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