C ++ (V) complex types (array, character array, string)

First, the array

Popular to say the array is a group of the same type of data, the definition of an array, is the definition of a group of data. For example, student achievement management system, you can not be a definition of a student's name, student's school, student achievement. This will not only cumbersome, but also the name for each variable.
So this thing array proposed in the C language. Said before the definition of an array, it is the definition of a group of data. So probably defined as follows:
int stu_num [50];
after such a definition, all of a sudden 50 defines a variable of type int, they are unified in an array inside.

Defines an array of
substantially define an array format is as follows:
type name array name [constant expressions];

Note that the [] constant in an expression used to indicate the number of elements in the array, this must be a constant or a constant expression, not a variable or an indeterminate value, or compile stage direct the error. Because after a good definition, the system immediately allocate space according to this number. If it is a variable, the system does not know how much space to allocate.

Naming an array with variable names are the same, only use numbers, letters, underscores, and numbers can not be done at the beginning.

Access elements in the array:
before using the array must be defined, needs to be accessed by index, specific access format is as follows:
array name [element index];
index of this element is zero-based, for example: int stu_num [60]; then the index is 0 to 59.
Note: This index can be a constant or may be variable, but must be accessed within the specified range, otherwise there will be access violation, resulting in the collapse of a major program low-level problems.

Initializing one-dimensional array:
①, the definitions for all time on the array element of the array initialization:
int stu_num [. 5] = {101, 102, 103, 104, 105};

②, in the definition of some elements when the array is initialized:
int stu_num [. 5] = {101, 102};
behind uninitialized default initial value 0
is actually: stu_num [0] = 101, stu_num [1] = 102 , stu_num [2] = stu_num [ 3] = stu_num [4] = 0

③, in the definition of the array when it is initialized to all elements of the array, the array need not be specified length:
int stu_num [] = {101, 102, 103, 104, 105};

Whether the definition of a specified length of time or do not specify the length, summed up on a rule, the definition of time to let the system know the length of the array can be, that is the definition that is sized.

Second, the array of characters

Array is an array of characters stored in character, that is, char type. For example: char stu_name [50];
Since the string itself is a combination of a character, so that the character array can be understood as a string. After the C ++ programming process will encounter a variety of operations on strings.

Initialization character array
mode ①: initializing defined
char stu_name [3] = { ' x', 'y', 'z'};
or char stu_name [] = { 'x ', 'y', 'z'}; // automatic initialization length, so that the compiler to the number (as an array)
or char stu_name [] = "xyz" ; // initialize the length automatically, to allow the compiler number (Like an array)
or char stu_name [3] = " xyz ";

Mode ②: are initialized after each character is defined;
stu_name [0] = 'X';
stu_name [. 1] = 'Y';
stu_name [2] = 'Z';

Input and output of the array of characters

//输入
char stu_name[10];
cin >> stu_name;
cout << stu_name<< endl;

//输出
char stu_name[10] = {'a', 'b', 'c', 'd', 'e', '\0', ' ', 'x', 'y', 'z'};
cout << stu_name<< endl;

C language standard library commonly used character manipulation functions

function purpose
strcpy(s1, s2); Copy the string to the string s2 s1.
strcat(s1, s2); Connection String s1 s2 to the end of the string.
strlen(s1); It returns the length of the string s1.
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.
strchr(s1, ch); It returns a pointer to the first character position of the string s1 of the first occurrence of ch.
strstr(s1, s2); It 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;
}

The difference between an array of characters and a string of
difference is more than a string array of characters marks the end of a character: '\ 0', ASCII code for this character is 0.
Is a valid character string marks the end of \ 0 character before. The number of bytes occupied by a string comprising the string marks the end \0of .

Third, the string

Standard C ++ class library provides a string type, supported above operation functions common character C language standard library, also provides convenient operation as follows:

#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;
}
Published 386 original articles · won praise 592 · views 720 000 +

Guess you like

Origin blog.csdn.net/wsp_1138886114/article/details/104273330