std::basic_string<CharT,Traits,Allocator>::data (3) - Linux Manuals
std::basic_string<CharT,Traits,Allocator>::data: std::basic_string<CharT,Traits,Allocator>::data
Command to display std::basic_string<CharT,Traits,Allocator>::data
manual in Linux: $ man 3 std::basic_string<CharT,Traits,Allocator>::data
NAME
std::basic_string<CharT,Traits,Allocator>::data - std::basic_string<CharT,Traits,Allocator>::data
Synopsis
const CharT* data() const; (until C++11)
const CharT* data() const noexcept; (1) (since C++11)
CharT* data() noexcept; (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. (until C++11)
If empty() returns true, the pointer is a non-null pointer that should not be dereferenced.
The returned array is null-terminated, that is, data() and c_str() perform the same function. (since C++11)
If empty() returns true, the pointer points to a single null character.
The pointer obtained 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().
1) Modifying the character array accessed through the const overload of data has undefined behavior.
2) Modifying the past-the-end null terminator stored at data()+size() to any value other than CharT() has undefined behavior.
Parameters
(none)
Return value
A pointer to the underlying character storage.
data()[i] == operator[](i) for every i in [0, size()). (until C++11)
data() + i == std::addressof(operator[](i)) for every i in [0, size()]. (since C++11)
Complexity
Constant.
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()));
}
See also
front accesses the first character
(public member function)
(C++11)
back accesses the last character
(public member function)
(C++11)
returns a non-modifiable standard C character array version of the string
c_str (public member function)
Pages related to std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::append (3) - std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::assign (3) - std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::at (3) - std::basic_string<CharT,Traits,Allocator>::at
- std::basic_string<CharT,Traits,Allocator>::back (3) - std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::basic_string (3) - std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::begin, (3) - std::basic_string<CharT,Traits,Allocator>::begin,
- std::basic_string<CharT,Traits,Allocator>::begin,std::basic_string<CharT,Traits,Allocator>::cbegin (3) - std::basic_string<CharT,Traits,Allocator>::begin,std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::c_str (3) - std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::capacity (3) - std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::clear (3) - std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::compare (3) - std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::copy (3) - std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::empty (3) - std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::end, (3) - std::basic_string<CharT,Traits,Allocator>::end,
- std::basic_string<CharT,Traits,Allocator>::end,std::basic_string<CharT,Traits,Allocator>::cend (3) - std::basic_string<CharT,Traits,Allocator>::end,std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::ends_with (3) - std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::erase (3) - std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::find (3) - std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of (3) - std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_first_of (3) - std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of (3) - std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of (3) - std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::front (3) - std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::get_allocator (3) - std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::insert (3) - std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::max_size (3) - std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::npos (3) - std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::operator+= (3) - std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::operator= (3) - std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[] (3) - std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view (3) - std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view
- std::basic_string<CharT,Traits,Allocator>::pop_back (3) - std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::push_back (3) - std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::rbegin, (3) - std::basic_string<CharT,Traits,Allocator>::rbegin,
- std::basic_string<CharT,Traits,Allocator>::rbegin,std::basic_string<CharT,Traits,Allocator>::crbegin (3) - std::basic_string<CharT,Traits,Allocator>::rbegin,std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, (3) - std::basic_string<CharT,Traits,Allocator>::rend,
- std::basic_string<CharT,Traits,Allocator>::rend,std::basic_string<CharT,Traits,Allocator>::crend (3) - std::basic_string<CharT,Traits,Allocator>::rend,std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::replace (3) - std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::reserve (3) - std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::resize (3) - std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::rfind (3) - std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit (3) - std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::size, (3) - std::basic_string<CharT,Traits,Allocator>::size,
- std::basic_string<CharT,Traits,Allocator>::size,std::basic_string<CharT,Traits,Allocator>::length (3) - std::basic_string<CharT,Traits,Allocator>::size,std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::starts_with (3) - std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::substr (3) - std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::swap (3) - std::basic_string<CharT,Traits,Allocator>::swap