| string::data |
public member function |
const char* data() const; |
|
Get string data
Returns a pointer to an array of characters with the same content as the string.
Notice that no terminating null character is appended (see member c_str for such a functionality).
The returned array points to an internal location which should not be modified directly in the program. Its contents are only granted to remain unchanged until the next call to a non-constant member function of the string object.
Parameters
none
Return Value
Pointer to an internal array containing the same content as the string.
Example
// string::data
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int length;
string str = "Test string";
char* cstr = "Test string";
if ( str.length() == strlen (cstr) )
{
cout << "str and cstr have the same length.\n";
length = str.length();
if ( memcmp (cstr, str.data(), length ) == 0 )
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.
|
Basic template member declaration
( basic_string<charT,traits,Allocator> )
const charT* data ( ) const;
|
See also
| string::copy | Copy sequence of characters from string (public member function) |