basic_string::c_str

式. "で終わる文字列として文字列の内容を変換します。

const value_type *c_str( ) const;

戻り値

呼び出しの C スタイル文字列バージョンへのポインター。ポインター値は非 const 関数を、basic_string オブジェクトのクラスのデストラクターが、呼び出した後で無効です。

解説

basic_stringchar<> C++ テンプレート クラスに属する String 型のオブジェクトは、null で終わる文字列ではありません。 空白文字「\0」は、.で特殊文字として使用されています。文字列の末尾を示すために文字列であれば、Object、String 型のオブジェクトで特別な意味を持たない、および他の文字など、文字列の一部だけである場合があります。 文字列定数に char* から自動変換がありますが、String クラスは 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 クラス