C++ string


C++ provides the following two types of string representations:

  • C-style string

  • The string class type introduced in C++

C-style string

C-style string originated from the C language and continues to be supported in C++. A string is actually a one-dimensional character array terminated with the null character '\0'. Therefore, a null-terminated string contains the characters that make up the string.

The following declaration and initialization creates a "Hello" string. The size of the character array is one more than the number of characters of the word "Hello" because a null character is stored at the end of the array.

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '
char greeting[] = "Hello";
'};

According to the array initialization rules, you can write the above statement as the following statement:

#include <iostream>

using namespace std;

int main ()
{
   char greeting[6] = {'H', 'e', 'l', 'l', 'o', '
Greeting message: Hello
'};    cout << "Greeting message: ";    cout << greeting << endl;    return 0; }

The following is the memory representation of the string defined in C/C++:

string_representation.jpg

Actually, you don't need to put the null character at the end of the string constant. The C++ compiler will automatically put '\0' at the end of the string when initializing the array. Let us try to output the above string:

#include <iostream>
#include <cstring>

using namespace std;

int main ()
{
   char str1[10] = "Hello";
   char str2[10] = "World";
   char str3[10];
   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;
}

When the above code is compiled and executed, it will produce the following results:

strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10

There are a large number of functions in C++ for operating with null Terminated strings: supports a wide range of functions that manipulate null-terminated strings:

serial numberfunction&purpose
1strcpy(s1, s2);
Copy string s2 to string s1.
2strcat(s1, s2);
Concatenate string s2 to the end of string s1.
3strlen(s1);
Returns the length of string s1.
4strcmp(s1, s2);
If s1 and s2 are the same, return 0; if s1< If s2, it returns less than 0; if s1>s2, it returns greater than 0.
5strchr(s1, ch);
Returns a pointer pointing to the first character ch in string s1 The location where it appears.
6strstr(s1, s2);
Returns a pointer pointing to the first string s2 in string s1 The position where it appears.

The following example uses some of the above functions:

#include <iostream>
#include <string>

using namespace std;

int main ()
{
   string str1 = "Hello";
   string str2 = "World";
   string str3;
   int  len ;

   // 复制 str1 到 str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;

   // 连接 str1 和 str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;

   // 连接后,str3 的总长度
   len = str3.size();
   cout << "str3.size() :  " << len << endl;

   return 0;
}

When the above code is compiled and executed, it will produce the following results:

str3 : Hello
str1 + str2 : HelloWorld
str3.size() :  10

String class in C++

The C++ standard library provides the string class type, which supports all the above operations, and also adds other more functions. We will study this class in the C++ standard library. Now let's take a look at the following example:

You may not understand this example thoroughly now because we have not discussed classes and objects so far. . So now you can just take a quick look at this example and come back to it later when you understand the object-oriented concepts.

rrreee

When the above code is compiled and executed, it produces the following results:

rrreee