1. Array
1.1 Overview
The so-called array is a collection that stores data elements of the same type
Feature 1: Each in the arrayThe data elements are all of the same data type
Feature 2: The array is composed ofcontiguous memorycomposed of positions
1.2 One-dimensional array
1.2.1 One-dimensional array definition method
Three ways to define a one-dimensional array:
数据类型 数组名[ 数组长度 ];
数据类型 数组名[ 数组长度 ] = { 值1,值2 ...};
数据类型 数组名[ ] = { 值1,值2 ...};
example
int main() {
//定义方式1
//数据类型 数组名[元素个数];
int score[10];
//利用下标赋值
score[0] = 100;
score[1] = 99;
score[2] = 85;
//利用下标输出
cout << score[0] << endl;
cout << score[1] << endl;
cout << score[2] << endl;
//第二种定义方式
//数据类型 数组名[元素个数] = {值1,值2 ,值3 ...};
//如果{}内不足10个数据,剩余数据用0补全
int score2[10] = {
100, 90,80,70,60,50,40,30,20,10 };
//逐个输出
//cout << score2[0] << endl;
//cout << score2[1] << endl;
//一个一个输出太麻烦,因此可以利用循环进行输出
for (int i = 0; i < 10; i++)
{
cout << score2[i] << endl;
}
//定义方式3
//数据类型 数组名[] = {值1,值2 ,值3 ...};
int score3[] = {
100,90,80,70,60,50,40,30,20,10 };
for (int i = 0; i < 10; i++)
{
cout << score3[i] << endl;
}
system("pause");
return 0;
}
Summary 1: The naming convention of the array name is consistent with the naming convention of the variable name, do not have the same name as the variable Summary
2: The subscript in the array starts from 0
1.2.2 One-dimensional array array name
Use of 1D array names :
- Can count the length of the entire array in memory
- You can get the first address of the array in memory
Example:
int main() {
//数组名用途
//1、可以获取整个数组占用内存空间大小
int arr[10] = {
1,2,3,4,5,6,7,8,9,10 };
cout << "整个数组所占内存空间为: " << sizeof(arr) << endl;
cout << "每个元素所占内存空间为: " << sizeof(arr[0]) << endl;
cout << "数组的元素个数为: " << sizeof(arr) / sizeof(arr[0]) << endl;
//2、可以通过数组名获取到数组首地址
cout << "数组首地址为: " << (int)arr << endl;
cout << "数组中第一个元素地址为: " << (int)&arr[0] << endl;
cout << "数组中第二个元素地址为: " << (int)&arr[1] << endl;
//arr = 100; 错误,数组名是常量,因此不可以赋值
system("pause");
return 0;
}
Note: The array name is a constant and cannot be assigned.
Summary 1: Print the array name directly, and you can view the first address of the memory occupied by the array.
Summary 2: Perform sizeof on the array name to get the size of the memory space occupied by the entire array
1.2.3 Bubble sort
Function: The most commonly used sorting algorithm, sorting the elements in the array
- Compare adjacent elements. If the first is bigger than the second, swap them both.
- Do the same for each pair of adjacent elements, and when done, find the first maximum value.
- Repeat the above steps, each time the number of comparisons -1, until no comparison is required
Example: Sort the array { 4,2,8,0,5,7,1,3,9 } in ascending order
int main() {
int arr[9] = {
4,2,8,0,5,7,1,3,9 };
for (int i = 0; i < 9 - 1; i++)
{
for (int j = 0; j < 9 - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < 9; i++)
{
cout << arr[i] << endl;
}
system("pause");
return 0;
}
1.3 Two-dimensional array
A two-dimensional array is an extra dimension added to a one-dimensional array.
1.3.1 Two-dimensional array definition method
Four ways to define a two-dimensional array:
数据类型 数组名[ 行数 ][ 列数 ];
数据类型 数组名[ 行数 ][ 列数 ] = { {数据1,数据2 } ,{数据3,数据4 } };
数据类型 数组名[ 行数 ][ 列数 ] = { 数据1,数据2,数据3,数据4};
数据类型 数组名[ ][ 列数 ] = { 数据1,数据2,数据3,数据4};
Suggestion: use the above 4 definition methodsThe second is more intuitive and improves the readability of the code
Example:
int main() {
//方式1
//数组类型 数组名 [行数][列数]
int arr[2][3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
//方式2
//数据类型 数组名[行数][列数] = { {数据1,数据2 } ,{数据3,数据4 } };
int arr2[2][3] =
{
{
1,2,3},
{
4,5,6}
};
//方式3
//数据类型 数组名[行数][列数] = { 数据1,数据2 ,数据3,数据4 };
int arr3[2][3] = {
1,2,3,4,5,6 };
//方式4
//数据类型 数组名[][列数] = { 数据1,数据2 ,数据3,数据4 };
int arr4[][3] = {
1,2,3,4,5,6 };
system("pause");
return 0;
}
Summary: When defining a two-dimensional array, if the data is initialized, the number of rows can be omitted
1.3.2 Two-dimensional array array name
- View the memory space occupied by a two-dimensional array
- Get the first address of the two-dimensional array
Example:
int main() {
//二维数组数组名
int arr[2][3] =
{
{
1,2,3},
{
4,5,6}
};
cout << "二维数组大小: " << sizeof(arr) << endl;
cout << "二维数组一行大小: " << sizeof(arr[0]) << endl;
cout << "二维数组元素大小: " << sizeof(arr[0][0]) << endl;
cout << "二维数组行数: " << sizeof(arr) / sizeof(arr[0]) << endl;
cout << "二维数组列数: " << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
//地址
cout << "二维数组首地址:" << arr << endl;
cout << "二维数组第一行地址:" << arr[0] << endl;
cout << "二维数组第二行地址:" << arr[1] << endl;
cout << "二维数组第一个元素地址:" << &arr[0][0] << endl;
cout << "二维数组第二个元素地址:" << &arr[0][1] << endl;
system("pause");
return 0;
}
Summary 1: The name of the two-dimensional array is the first address of the array.
Summary 2: When sizeof is performed on the name of the two-dimensional array, the memory space occupied by the entire two-dimensional array can be obtained
1.3.3 Two-dimensional array application case
Exam score statistics:
Case description: There are three students (Zhang San, Li Si, Wang Wu), the scores in one test are as follows, please output the total scores of the three students respectively
language | math | English | |
---|---|---|---|
Zhang San | 100 | 100 | 100 |
Li Si | 90 | 50 | 100 |
Wang Wu | 60 | 70 | 80 |
Reference answer:
int main() {
int scores[3][3] =
{
{
100,100,100},
{
90,50,100},
{
60,70,80},
};
string names[3] = {
"张三","李四","王五" };
for (int i = 0; i < 3; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += scores[i][j];
}
cout << names[i] << "同学总成绩为: " << sum << endl;
}
system("pause");
return 0;
}
2. String
C++ provides the following two types of string representations:
- C-style strings
- The string class type introduced by C++
2.1 C-style strings
C-style strings originated in the C language and continue to be supported in C++. Strings are actually one-dimensional character arrays terminated with the null character \0. Thus, a null-terminated string contains the characters that make up the string.
String representations in C/C++
Actually, you don't need to put the null character at the end of a string constant. The C++ compiler will automatically put \0 at the end of the string when initializing the array. Let's try to output the above string:
#include <iostream>
using namespace std;
int main ()
{
char site[7] = {
'A', 'B', 'C', 'D', 'E', 'F', '\0'};
cout << "char类型: " << site << endl;
return 0;
}
There are a large number of functions in C++ for manipulating null-terminated strings:
strcpy(s1, s2)
: Copy string s2 to string s1.
strcat(s1, s2)
: Concatenates string s2 to the end of string s1. The connection string can also use the + sign, for example:
strlen(s1)
: returns the length of the string s1.
strcmp(s1, s2)
: If s1 and s2 are the same, return 0; if s1<s2, return a value less than 0; if s1>s2, return a value greater than 0.
strchr(s1, ch)
: Returns a pointer to the first occurrence of the character ch in the string s1.
strstr(s1, s2)
: Returns a pointer to the first occurrence of string s2 in string s1.
Example:
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[13] = "baidu";
char str2[13] = "google";
char str3[13];
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 execution results are as follows:
strcpy( str3, str1) : baidu
strcat( str1, str2): baidugoogle
strlen(str1) : 11
2.2 The 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.
Example:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "baidu";
string str2 = "google";
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;
}
The execution results are as follows:
str3 : baidu
str1 + str2 : baidugoogle
str3.size() : 11