basic_string::c_str
Konwertuje zawartość ciągu jako styl c ciąg zakończony znakiem null.
const value_type *c_str( ) const;
Wartość zwracana
Wskaźnik do wersji styl c uruchamiającym ciąg.Po wywołaniu funkcji niż const, włączając destruktora klasy basic_string w obiekcie nie jest prawidłową wartość wskaźnika.
Uwagi
Obiekty typu ciąg należące do klasy szablonów języka C++ w basic_string <char> nie musi koniecznie null są zakończone.Znak null '\0' jest używana jako znak specjalny w ciągu C, aby oznaczyć koniec ciągu, ale nie ma specjalnego znaczenia w obiekcie typu ciąg i może być częścią ciągu podobnie jak inne znaki.Istnieje automatyczna konwersja z const char * do ciągów, ale ciąg klasy nie przewiduje automatycznego konwersji ciągów c styl obiektów typu basic_string <char>.
Zwrócony ciąg c stylu nie powinny być modyfikowane, to może unieważnić wskaźnik do ciągu lub usunięty, ponieważ ciąg ograniczony okres istnienia i właścicielem jest ciąg klasy.
Przykład
// 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;
}
Wymagania
Nagłówek: <string>
Obszar nazw: std