The string class of the standard library provides 3 member functions to get a character array of type C from a string: C_str (), data (), copy (P,n).
1. C_STR (): Generates a const char* pointer to an array terminated with a null character.
Note:
① the data for This array is temporary, and when a member function that changes the data is called, the data is invalidated. Therefore, it is either converted first, or the data is copied to the memory that the user can manage. Attention. look at the following example:
constchar* c;string s="1234";c = s.c_str(); cout<<c<<endl; //输出:1234s="abcd";cout<<c<<endl; //输出:abcd |
If you continue to use the C pointer above, the resulting error will be unthinkable. Just like this: 1234 becomes ABCD
In fact, the above C = S.c_str (); Not a good habit. Since the content of the C pointer is easy to invalidate, we should follow the above method, how to copy the data? This will use the strcpy and other functions (recommended).
//const char* C;//① // char* c; //② //char c[20]; char * c= new char [20]; string s= //c = S.c_str (); strcpy cout<<c<<endl; //output: 1234 s= "ABCD" cout<<c<<endl; //output: 1234 |
Note: You can no longer ① as shown in the above, const also how to write the value Ah, nor ②, using the uninitialized local variable "C", the operation will be wrong.
②C_STR () Returns a client-readable pointer to a character array, without manually releasing or deleting the pointer.
2. Data (): Similar to C_str (), but the returned array is not terminated with a null character.
3. Copy (P,n,size_type _off = 0): Copies at most n characters from a string type object to the space pointed to by the character Pointer p. The default starts with the first character, but you can also specify the starting position (remember starting with 0). Returns the characters that are actually copied from the object. ------ the user to ensure that the space that P points to is sufficient to hold n characters .
// basic_string_copy.cpp// compile with: /EHsc /W3#include <string>#include <iostream>intmain( ){ usingnamespacestd; string str1 ( "1234567890"); basic_string <char>::iterator str_Iter; chararray1 [ 20 ] = { 0 }; chararray2 [ 10 ] = { 0 }; basic_string <char>:: pointer array1Ptr = array1; basic_string <char>:: value_type *array2Ptr = array2; cout << "The original string str1 is: "; for( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ ) cout << *str_Iter; cout << endl; basic_string <char>:: size_type nArray1; // Note: string::copy is potentially unsafe, consider // using string::_Copy_s instead. nArray1 = str1.copy ( array1Ptr , 12 ); // C4996 cout << "The number of copied characters in array1 is: " << nArray1 << endl; cout << "The copied characters array1 is: " << array1Ptr << endl; basic_string <char>:: size_type nArray2; // Note: string::copy is potentially unsafe, consider // using string::_Copy_s instead. nArray2 = str1.copy ( array2Ptr , 5 , 6 ); // C4996 cout << "The number of copied characters in array2 is: " << nArray2 << endl; cout << "The copied characters array2 is: "<< array2Ptr << endl; ////注意一定要使array3有足够的空间 //char array3[5]={0}; //basic_string<char>::pointer array3Ptr=array3; //basic_string<char>::size_type nArray3; //nArray3 = str1.copy(array3,9); //错误!!!! //cout<<"The number of copied characters in array3 is: " // <<nArray3<<endl; //cout<<"The copied characters array3 is: "<<array3Ptr<<endl;} |
The last commented out section, although the compilation is not error, but the runtime will produce an error: Stack around the variable ' array3 ' was corrupted.
Use of C_str (), data (), copy (P,n) functions in string