cplusplus.com
  C++ Library Reference : Strings library : string : data
- -
º¯Êý¿â
C++º¯Êý¿â
Cº¯Êý¿â
C++º¯Êý¿â
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Strings library
char_traits
classes:
· string
global functions:
· getline
· operator+
· operator<<
· operator>>
· comparison operators
· swap
string
· string::string
member constants:
· string::npos
member functions:
· string::append
· string::assign
· string::at
· string::begin
· string::capacity
· string::clear
· string::compare
· string::copy
· string::c_str
· string::data
· string::empty
· string::end
· string::erase
· string::find
· string::find_first_not_of
· string::find_first_of
· string::find_last_not_of
· string::find_last_of
· string::get_allocator
· string::insert
· string::length
· string::max_size
· string::operator+=
· string::operator=
· string::operator[]
· string::push_back
· string::rbegin
· string::rend
· string::replace
· string::reserve
· string::resize
· string::rfind
· string::size
· string::substr
· string::swap

-

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)
string::c_str Get C string equivalent (public member function)
string::assign Assign content to string (public member function)
© Copyright © 2007-2008 ¿áÇÚÍø All Rights Reserved