basic_string::c_str

转换字符串的内容。式 C.,Null 终止的字符串。

const value_type *c_str( ) const;

返回值

为名为的字符串中的 C 样式版本的指针。指针值。调用非为 Const 函数后包括无效,析构函数,在对象的 basic_string 的类。

备注

C++ 模板属于字符串类型的对象类 basic_string<char> 不一定是终止空的。 空字符“\0 ' is used as a special character in a C - 标记字符串末尾的字符串,但没有特殊含义在类型字符串对象,并且可能为字符串的一些部分与其他字符。 从具有常数 char* 的自动转换为字符串中,但字符串类不提供自动转换从 C 样式字符串到 **basic_string<char>**类型对象。

不应修改,因为这可能无效指针指向字符串,或者删除这返回的 C 样式字符串,因为字符串具有有限生存期和由类的字符串。

示例

// basic_string_c_str.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

请参见

参考

basic_string 类