const CharT* data() const; | (1) | |
CharT* data(); | (2) | (since C++17) |
Returns a pointer to the underlying array serving as character storage. The pointer is such that the range [data(); data() + size())
is valid and the values in it correspond to the values stored in the string.
The returned array is not required to be null-terminated. If | (until C++11) |
The returned array is null-terminated, that is, | (since C++11) |
The pointer obtained from data()
may be invalidated by:
operator[]()
, at()
, front()
, back()
, begin()
, end()
, rbegin()
, rend()
. data
has undefined behavior.data()+size()
has undefined behavior.(none).
A pointer to the underlying character storage.
data()[i] == operator[](i) for every i in [0, size()) . | (until C++11) |
data() + i == &operator[](i) for every i in [0, size()] . | (since C++11) |
Constant.
(none) | (until C++11) |
noexcept specification: noexcept | (since C++11) |
#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())); }
(C++11)
| accesses the first character (public member function) |
(C++11)
| accesses the last character (public member function) |
returns a non-modifiable standard C character array version of the string (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/string/basic_string/data