std::basic_..._view::data(3)
β¨ NAME β¨
std::basic_string_view::data - std::basic_string_view::data
okay so welcome to the std::basic_..._view::data(3) page from the C++ Standard Libary (yes, that spelling, we keep it π
). It's giving pointer-to-the-underlying-character-array energy, and we're obsessed. no cap.
π¦ Synopsis π¦
Here's the signature, fresh out of the standard-lib oven:
constexpr const_pointer data() const noexcept; (since C++17)
It returns a pointer to the underlying character array. The pointer is such that the range [data(), data() + size()) is valid and the values in it correspond to the values of the view. β¨
π Parameters π
(none) β not a single parameter, bestie π
πΈ Return value πΈ
A pointer to the underlying character array. That's the whole vibe π«Ά
π« Complexity π«
Constant. periodt.
π§ Notes π§
Big caution flag, bestie π©: unlikestd::basic_string::data()and string literals,std::basic_string_view::data()returns a pointer to a buffer that is not necessarily null-terminated, for example a substring view (e.g. fromremove_suffix). Therefore, it is typically a mistake to passdata()to a routine that takes just aconst CharT*and expects a null-terminated string. Like, do not send it to that function, it will not slay ππ.
π Example π
// Run this code
#include <cstring>
#include <cwchar>
#include <iostream>
#include <string>
#include <string_view>
int main()
{
std::wstring_view wcstr_v = L"xyzzy";
std::cout << std::wcslen(wcstr_v.data()) << '\n';
// OK: the underlying character array is null-terminated
char array[3] = {'B', 'a', 'r'};
std::string_view array_v(array, sizeof array);
// std::cout << std::strlen(array_v.data()) << '\n';
// error: the underlying character array is not null-terminated
std::string str(array_v.data(), array_v.size()); // OK
std::cout << std::strlen(str.data()) << '\n';
// OK: the underlying character array of a std::string is always
null-terminated
}
π Output π
5
3
π See also π
frontβ accesses the first character (public member function)backβ accesses the last character (public member function)dataβ returns a pointer to the first character of a string (public member function ofstd::basic_string<CharT,Traits,Allocator>)
mwah π this page says it came from http://cppreference.com and it's dated 2024.06.10. bye bestie, go check that null terminator π¦π
π¦ 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