Skip to main content

std::string data() method

// (1) Const version
constexpr const CharT* data() const noexcept;

// (2) Non const version
constexpr CharT* data() noexcept;

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 null-terminated, that is, data() and c_str() perform the same function.

important

If empty() returns true, the pointer points to a single null character.

Invalidation

The pointer obtained from data() may be invalidated by:

Undefined Behavior
  • (1) Modifying the character array accessed through the const overload of data results in undefined behavior.
  • (2) Modifying the past-the-end null terminator stored at data() + size() to any value other than CharT() is undefined behavior.

Parameters

(none)

Return value

A pointer to the underlying character storage.

data()[i] == operator[](i) for every i in [ 0, size() ).

Complexity

Constant - O(1).

Notes

c_str() and data() perform the same function. (since C++11)

Example

Main.cpp
#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()));
}
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::string data() method

// (1) Const version
constexpr const CharT* data() const noexcept;

// (2) Non const version
constexpr CharT* data() noexcept;

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 null-terminated, that is, data() and c_str() perform the same function.

important

If empty() returns true, the pointer points to a single null character.

Invalidation

The pointer obtained from data() may be invalidated by:

Undefined Behavior
  • (1) Modifying the character array accessed through the const overload of data results in undefined behavior.
  • (2) Modifying the past-the-end null terminator stored at data() + size() to any value other than CharT() is undefined behavior.

Parameters

(none)

Return value

A pointer to the underlying character storage.

data()[i] == operator[](i) for every i in [ 0, size() ).

Complexity

Constant - O(1).

Notes

c_str() and data() perform the same function. (since C++11)

Example

Main.cpp
#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()));
}
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.