A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/string/basic_string/data/ below:

public member function

<string>

std::basic_string::data
const charT* data() const;
const charT* data() const noexcept;

Get string data

Returns a pointer to an array that contains the same sequence of characters as the characters that make up the value of the

basic_string

object.

Accessing the value at data()+size() produces undefined behavior: There are no guarantees that a null character terminates the character sequence pointed by the value returned by this function. See basic_string::c_str for a function that provides such guarantee.

A program shall not alter any of the characters in this sequence.


Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the

basic_string

object.

This array includes the same sequence of characters that make up the value of the basic_string object plus an additional terminating null-character (charT()) at the end.

The pointer returned points to the internal array currently used by the basic_string object to store the characters that conform its value.

Both basic_string::data and basic_string::c_str are synonyms and return the same value.



The pointer returned may be invalidated by further calls to other member functions that modify the object.

Parameters none

Return Value A pointer to the c-string representation of the basic_string object's value.

charT is basic_string's character type (i.e., its first template parameter).



Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// string::data
#include <iostream>
#include <string>
#include <cstring>

int main ()
{
  int length;

  std::string str = "Test string";
  char* cstr = "Test string";

  if ( str.length() == std::strlen(cstr) )
  {
    std::cout << "str and cstr have the same length.\n";

    if ( memcmp (cstr, str.data(), str.length() ) == 0 )
      std::cout << "str and cstr have the same content.\n";
  }
  return 0;
}

Output:
str and cstr have the same length.
str and cstr have the same content.


Complexity, iterator, access, exceptions Unspecified or contradictory specifications.

Complexity Constant.

Iterator validity No changes.

Data races The object is accessed.

Exception safetyNo-throw guarantee: this member function never throws exceptions.


See also
basic_string::c_str
Get C-string equivalent
basic_string::copy
Copy sequence of characters from string (public member function)
basic_string::operator[]
Get character of string (public member function)
basic_string::front
Access first character (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4