Wu Yuxiong - born natural C ++ language study notes: C ++ string

C ++ provides two types of string representation: 
C-style string 
C ++ introduced string class type
C-style strings originated in the C language, and continue to be supported in C ++. String actually used null character ' \ 0 ' one-dimensional array of characters terminated. Therefore, to a null -terminated string that contains the character string. 
The following statements create and initialize a " the Hello " string. Since the end of the array of stores null character, so the size of the array of characters than the word " the Hello " more than a number of characters.
char Greeting [ . 6 ] = { ' H ' , ' E ' , ' L ' , ' L ' , ' O ' , ' \ 0 ' };
Based array initialization rule may be written to the above statements the following statement:
 char Greeting [] = " the Hello " ; 
C / C ++ memory strings defined in said:

In fact, you do not need the null character at the end of the string constants. When the C ++ compiler will initialize the array, automatically ' \ 0 ' on the end of the string. 
#include <the iostream> the using namespace STD; int main () 
{ char Greeting [ . 6 ] = { ' H ' , ' E ' , ' L ' , ' L ' , ' O ' , ' \ 0 ' }; 
   COUT << " the Greeting the Message: " ;
 
 
 

   
 Greeting << << endl; 
 
   return  0 ; 
} 
when the above code is compiled and executed, it produces the following results: 

the Greeting Message: the Hello
C ++ function is used in a large number of operations to null -terminated string
 . 1     strcpy (S1, s2); 
copy the string to the string s2 s1. 
2     strcat (s1, s2); 
connection string s1 s2 to the end of the string. 
. 3     strlen (s1); 
returns the length of the string of s1. 
. 4     strcmp (s1, s2); 
If s1 and s2 are the same, then return 0 ; if s1 <s2, the return value is less than 0 ; if s1> s2, the return value is greater than 0 .
. 5     the strchr (s1, ch); 
Returns a pointer to the first character position of the string s1 of the first occurrence of ch. 
. 6     Strstr (s1, s2); 
Returns a pointer to the location of the string s1 s2 of the first occurrence of the string.
#include <iostream>
#include <cstring>
 
using namespace std;
 
int main ()
{
   char str1[11] = "Hello";
   char str2[11] = "World";
   char str3[11];
   int  len ;
 
   // 复制 str1 到 str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;
 
   // 连接 str1 和 str2
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;
 
   // 连接后,str1 的总长度
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;
 
   return 0;
}
C ++ is class String 
#include <the iostream> 
#include < String > the using namespace STD; int main () 
{ String str1 = " the Hello " ;
    String str2 = " World " ;
    String str3;
    int   len; // copy str1 to str3 
   = Str3 str1; 
   COUT << " Str3: " << Str3 << endl; // connected str1 and str2 
   Str3 str1 + = str2;
 
 
 

   
 
   
 
   
   COUT << " str1 + str2: " << str3 << endl; 
 
   // connection, the total length of str3 
   len = str3.size (); 
   COUT << " str3.size ():   " << len << endl ; 
 
   return  0 ; 
} 
when the above code is compiled and executed, it produces the following results: 
Str3: the Hello 
str1 + str2: the HelloWorld 
str3.size ():   10

 

Guess you like

Origin www.cnblogs.com/tszr/p/12148646.html