basic_string::data
将字符串的内容转换为字符数组。
const value_type *data( ) const;
返回值
指向包含字符串内容的数组中第一个元素的指针,或空数组的,则无法移除引用的非 null 指针。
备注
C++ 模板属于类 basic_string <char> Null 终止的字符串类型的对象。 空字符,因为不到的添,数据 的返回类型不是有效的 C# 字符串。 空字符“\0 ' is used as a special character in a C - 标记字符串末尾的字符串,但是,没有特殊含义在类型字符串对象,并且可能为字符串对象的任何其他部分字符。
从具有常数 char* 的自动转换为字符串中,但字符串类不提供自动转换从 C 样式字符串到 **basic_string <char>**类型对象。
不应修改,因为这可能无效指针指向字符串,或者删除这返回的字符串中,因为字符串具有有限生存期和由类的字符串。
示例
// basic_string_data.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
string str1 ( "Hello world" );
cout << "The original string object str1 is: "
<< str1 << endl;
cout << "The length of the string object str1 = "
<< str1.length ( ) << endl << endl;
// Converting a string to an array of characters
const char *ptr1 = 0;
ptr1= str1.data ( );
cout << "The modified string object ptr1 is: " << ptr1
<< endl;
cout << "The length of character array str1 = "
<< strlen ( ptr1) << endl << endl;
// Converting a string to a C-style string
const char *c_str1 = str1.c_str ( );
cout << "The C-style string c_str1 is: " << c_str1
<< endl;
cout << "The length of C-style string str1 = "
<< strlen ( c_str1) << endl << endl;
}
要求
标头:< 字符串>
命名空间: std