Latest web development tutorials

C ++ string

C ++ provides a string representation of the following two types:

  • C-style strings
  • C ++ string class type introduced

C-style strings

C-style strings originated in the C language and C ++ continues to be supported. String is actually usednull character '\ 0' one-dimensional array of characters terminated.Therefore, a null-terminated string that contains the characters of the string.

The following statements create and initialize a "Hello" string. Since the end of the array to store the null character, so the size of the array of characters than the word "Hello" is more than a number of characters.

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

Based array initialization rules, you can write the above statement the following statement:

char greeting[] = "Hello";

The following is a string of C / C ++ defined in memory, he said:

C / C ++ string representation

In fact, you do not need thenullcharacter at the end of the string constant. When the C ++ compiler array initialization, automatically '\ 0' on the end of the string. Let's try the above output string:

#include <iostream>

using namespace std;

int main ()
{
   char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

   cout << "Greeting message: ";
   cout << greeting << endl;

   return 0;
}

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

Greeting message: Hello

C ++ has a lot of functions for manipulating null-terminated string of: supports a wide range of functions that manipulate null-terminated strings:

序号函数 & 目的
1strcpy(s1, s2);
复制字符串 s2 到字符串 s1。
2strcat(s1, s2);
连接字符串 s2 到字符串 s1 的末尾。
3strlen(s1);
返回字符串 s1 的长度。
4strcmp(s1, s2);
如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回小于 0;如果 s1>s2 则返回大于 0。
5strchr(s1, ch);
返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。
6strstr(s1, s2);
返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。

The following example uses some of the above functions:

#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 produces the following results:

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

In C ++ String class

C ++ standard library provides astring class type supports all the operations described above, and also adds many more features.We will learn C ++ standard library class, now let's take a look at the following examples:

Now you may not yet be a thorough understanding of this example, because so far we have not discussed classes and objects. After so now you can just rough look to this example, and so understand object-oriented concepts come back around and understand this instance.

#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 produces the following results:

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