函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <cmath> //for pow(), sqrt(), ...
//#include <math.h>
#include <cstdlib> //c-style standard library functions, for rand(), srand(), ..
#include <ctime> //for time(0)
using namespace std;

/*
第六章 函数
常用库函数
自定义函数:语法格式,四种形式
函数重载
递归函数
*/

//递归函数:自己直接或间接调用自己: A->A; A->B, B->A

/*
n! = 1*2*...*(n-1)*n

n! = 1, n=0
n*(n-1)!, n>0
*/

int factorial2(int n)
{
int result = 1;
for(int i = 1; i <= n; i++)
{
result = result * i;// result *= i;
}
return result;
}

int factorial(int n)
{
if( n == 0 )
{
return 1;
}
else
{
return n * factorial( n - 1 );
}
}

int main()
{
for(int n = 1; n <= 10 ; n++)
{
//cout << n << "! = " << factorial2(n) << endl;
cout << n << "! = " << factorial(n) << endl;
}
return 0;
}

//函数重载:多个函数,函数名相同,形参列表不同(形参的数量、类型、顺序)

void test(int a, int b)
{
cout << "In test(int a, int b), a = " << a << " , b = " << b << endl;
}

void test(double a, double b)
{
cout << "In test(double a, double b), a = " << a << " , b = " << b << endl;
}

int main4()
{
test(2, 3);
test(2.3, 3.4);
return 0;
}

/*
自定义函数:
返回类型 函数名(形参列表)
{
//函数体
}

形参:形式参数,仅用于函数定义/声明
实参:实际参数,仅用于函数调用

四种形式:返回(有/无) + 形参(有/无)

使用顺序:定义-调用, 声明-调用-定义

调用格式: 函数名(实参列表)

函数调用流程:
第一步:暂停和转向;
第二步:传参(三种传参方式:传值,传地址,传引用);
第三步:执行;
第四步:返回
*/

void sayHello()//函数定义
{
cout << "Hello world!" << endl;
}

void showSum(int a, int b)//函数定义
{
cout << a << " + " << b << " = " << a + b << endl;
}

double PI()
{
return 3.1415926;
}

int sum(int a, int b)
{
int c;
c = a + b;
return c;
//return a + b;
}

int mul(int a, int b);//函数声明

int main3()
{
//sayHello();//函数调用
//showSum( 2, 3);//函数调用
//cout << PI() << endl;
//double r = 2;
//double area = PI() * r * r;
//cout << "area = " << area << endl;
//cout << sum(2,3) << endl;
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
//int c = sum(a, b);
int c = mul(a, b);//函数调用
cout << "The result is " << c << endl;
return 0;
}


int mul(int a, int b)//函数定义
{
return a * b;
}

//随机数:<cstdlib>, rand(), srand()
//rand(): 得到一个[0,RAND_MAX]中的一个随机数
//srand(): seed of random number,设置随机数种子
//[a,b]: a + rand() % ( b - a + 1 )

int main2()
{
cout << "RAND_MAX = " << RAND_MAX << endl;
srand(time(0));
for(int i = 1; i <= 10; i++)
{
//cout << rand() << "\t";
//cout << rand() % 10 << "\t";
//cout << rand() % 5 << "\t";//[0,4]
cout << 1 + rand() % 5 << "\t";//[1,5]
}

return 0;
}


//数学库函数 <cmath> <math.h>
//pow(a,b): 求a的b次方 , power
//sqrt(a): 求非负实数a的算术平方根


int main1()
{
cout << pow(2, 3) << endl;
cout << pow(2.3, 3.4) << endl;
cout << pow(-2, 2) << endl;
cout << pow(4, 0.5) << endl;
cout << sqrt(4) << endl;
//cout << sqrt(-4) << endl;//nan: not a number
return 0;
}

数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
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;
//一个int整型占4个字节


//可以通过数组名查看数组首地址
cout << "数组首地址为:" << arr << endl;
//地址一般为16进制,若想将其变成十进制,则改成(int)arr
cout << "数组中第一个元素的地址为:" << (int)&arr[0] << endl;
cout << "数组中第二个元素的地址为:" << (int)&arr[1] << endl;

//数组名是个常量,不可以进行赋值

system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include <array>
#include <vector>
using namespace std;

//第7章 array和vector

//vector: <vector>, 变长数组
//常用操作:整体赋值,size(),push_back(),pop_back(),resize()

void showVector(vector<int> v)
{
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}

int main()
{
vector<int> a;
cout << "At the beginning, a.size() = " << a.size() << endl;

for(int i = 1; i <= 10; i++)
{
a.push_back(i*2);
cout << "After i = " << i << " , a.size() = " << a.size() << endl;
}

showVector(a);

for(int i = 1; i <= 5; i++)
{
a.pop_back();
cout << "After i = " << i << " , a.size() = " << a.size() << endl;
}

showVector(a);

a.resize(10);
cout << "After a.resize(10): " << endl;
showVector(a);

a.resize(3);
cout << "After a.resize(3): " << endl;
showVector(a);

return 0;
}

int main8()
{
vector<int> a(3,2);
vector<int> b(4,5);
showVector(a);
showVector(b);
b = a;
cout << "After b = a :" << endl;
showVector(a);
showVector(b);
return 0;
}

int main7()
{
vector<int> a;//长度为0
vector<int> b(3);//长度为3,默认值都为0
vector<int> c(4,5);//长度为4,初始值都为5
vector<int> d = {1,2,3,4,5};//长度为初始值数量5
cout << "a.size() = " << a.size() << endl;
cout << "b.size() = " << b.size() << endl;
cout << "c.size() = " << c.size() << endl;
cout << "d.size() = " << d.size() << endl;

showVector(a);
showVector(b);
showVector(c);
showVector(d);
return 0;
}

//array: <array> , 定长数组
//常用操作:整体赋值,size(),fill(),swap()

void showMatrix(int mat[][3], int rowNum)
{
for(int i = 0; i < rowNum; i++)
{
for(int j = 0; j < 3; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}

void showMatrix(array< array<int,3> , 2 > mat)
{
for(int i = 0; i < mat.size(); i++)
{
for(int j = 0; j < mat[i].size(); j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}


int main6()
{
int mat[2][3] = {1,2,3,4,5,6};
array< array<int,3> , 2 > a = {11,12,13,14,15,16};
//array< array< array<int,4> ,3> , 2 > b;

//showMatrix(mat, 2);
showMatrix(a);

/*
cout << "a.size() = " << a.size() << endl;
cout << "a[0].size() = " << a[0].size() << endl;
cout << "a[1].size() = " << a[1].size() << endl;
cout << a[0][0] << " " << a[0][1] << " " << a[0][2] << endl;
cout << a[1][0] << " " << a[1][1] << " " << a[1][2] << endl;
*/


return 0;
}

void showArray(int arr[], int len)
{
cout << "------------" << endl;
for(int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

void showArray(array<int,3> arr)
{
cout << "------------" << endl;
for(int i = 0; i < 3; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

int main5()
{
//array<int,3> a = {1,2,3};
//array<int,3> b = {4,5,6};
const int N = 3;
array<int,N> a = {1,2,3}, b = {4,5,6};

showArray(a);
showArray(b);

//a.swap(b);
b.swap(a);
cout << "After swapping:" << endl;
showArray(a);
showArray(b);

return 0;
}

int main4()
{
array<int,3> a = {0};
cout << "a.size() = " << a.size() << endl;

showArray(a);

a.fill(2);

showArray(a);

return 0;
}

int main3()
{
int a[3] = {1,2,3};
int b[3];
//b = a;//error: invalid array assignment

for(int i = 0; i < 3; i++)
{
a[i] = 2;
}

array<int,3> c = {4,5,6};
array<int,3> d;

showArray(c);
showArray(d);

d = c;
cout << "After d = c:" << endl;
showArray(c);
showArray(d);

return 0;
}

int main2()
{
//int a[3] = {1,2,3};
int a[3] = {1};
//array<int,3> b = {4,5,6};
array<int,3> b = {4};

showArray(a,3);
showArray(b);


return 0;
}

int main1()
{
int a[3];
array<int,3> b;

a[0] = 1; a[1] = 2; a[2] = 3;
b[0] = 4; b[1] = 5; b[2] = 6;

int sum1 = a[0] + a[1] + a[2];
int sum2 = b[0] + b[1] + b[2];

cout << a[0] << " " << a[1] << " " << a[2] << endl;
cout << b[0] << " " << b[1] << " " << b[2] << endl;

for(int i = 0; i < 3; i++)
{
cout << a[i] << " ";
}
cout << endl;

for(int i = 0; i < 3; i++)
{
cout << b[i] << " ";
}
cout << endl;

return 0;
}

vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream> //Vector
#include <iomanip>
#include <array>
#include <vector>
using namespace std;

/*vector< int > a[5];
表示的是a是一个数组,类型是vector< int > ,数组大小是5
也就是说这个是一维固定,二维变化的变长数组,因为一维的每个元素都是vector类型

vector< int > b(5);
表示的是b是一个vector,初始化长度为5,默认初始化为0*/

int n=2,m=3;
vector<vector<int>>b(n,vector<int>(m,2));//2*3的数组,元素全是2
vector<int>a[10005];
a[0].push_back();

vector<int>a{3,4};
a.insert(a.begin(),2);//在第一个元素前插入一个2的元素
for(int i : a)
{
cout<<i<<' ';//2 3 4
}
a.erase(a.begin(),a.begin()+1);//清除数组a的第一个元素 区间大部分是左闭右开所以只清除第一个元素
a.clear()//清除数组a的元素





void showVector(vector<int> v)
{
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}

int main()
{
vector<int> a;
cout << "At the beginning, a.size() = " << a.size() << endl;

for (int i = 1; i <= 10; i++)
{
a.push_back(i+1); //增加一个单位长度并将其中的值赋值为i+1
a.emplace_back(i+1);
cout << "After i = " << i << " , a.size() = " << a.size() << endl;
}

showVector(a);

for (int i = 1; i <= 5; i++)
{
a.pop_back(); //减少一个单位长度
cout << "After i = " << i << " , a.size() = " << a.size() << endl;
}

showVector(a);

a.resize(10); //直接将数组长度变为10
cout << "After a.resize(10): " << endl;
showVector(a);

a.resize(3);
cout << "After a.resize(3): " << endl;
showVector(a);
return 0;
}
int main()
{
vector<int> a(3,2); //将数组a中的3个元素赋值为2
vector<int> b(4,5);
showVector(a);
showVector(b);
b = a;
cout << "After b = a :" << endl;
showVector(a);
showVector(b);
return 0;
}
int main7()
{
vector<int> a;//长度为0
vector<int> b(3);//长度为3,默认值都为0
vector<int> c(4,5);//长度为4,初始值都为5
vector<int> d = {1,2,3,4,5};//长度为初始值数量5
cout << "a.size() = " << a.size() << endl;
cout << "b.size() = " << b.size() << endl;
cout << "c.size() = " << c.size() << endl;
cout << "d.size() = " << d.size() << endl;
vector<int>a(b);//将b的值copy到a中
return 0;
}

array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//array: <array> , 定长数组 
//常用操作:整体赋值,size(),fill(),swap()

fill(a.begin(),a.end(),0);
void showMatrix(int mat[][3], int rowNum)
{
for(int i = 0; i < rowNum; i++)
{
for(int j = 0; j < 3; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}

void showMatrix(array< array<int,3> , 2 > mat)
{
for(int i = 0; i < mat.size(); i++)
{
for(int j = 0; j < mat[i].size(); j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}


int main6()
{
int mat[2][3] = {1,2,3,4,5,6};
array< array<int,3> , 2 > a = {11,12,13,14,15,16};
//array< array< array<int,4> ,3> , 2 > b;

//showMatrix(mat, 2);
showMatrix(a);

/*
cout << "a.size() = " << a.size() << endl;
cout << "a[0].size() = " << a[0].size() << endl;
cout << "a[1].size() = " << a[1].size() << endl;
cout << a[0][0] << " " << a[0][1] << " " << a[0][2] << endl;
cout << a[1][0] << " " << a[1][1] << " " << a[1][2] << endl;
*/


return 0;
}

void showArray(int arr[], int len)
{
cout << "------------" << endl;
for(int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

void showArray(array<int,3> arr)
{
cout << "------------" << endl;
for(int i = 0; i < 3; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

int main5()
{
//array<int,3> a = {1,2,3};
//array<int,3> b = {4,5,6};
const int N = 3;
array<int,N> a = {1,2,3}, b = {4,5,6};

showArray(a);
showArray(b);

//a.swap(b);
b.swap(a);
cout << "After swapping:" << endl;
showArray(a);
showArray(b);

return 0;
}
int main4()
{
array<int,3> a = {0};
cout << "a.size() = " << a.size() << endl;

showArray(a);

a.fill(2);

showArray(a);

return 0;
}

int main3()
{
int a[3] = {1,2,3};
int b[3];
//b = a;//error: invalid array assignment

for(int i = 0; i < 3; i++)
{
a[i] = 2;
}

array<int,3> c = {4,5,6};
array<int,3> d;

showArray(c);
showArray(d);

d = c;
cout << "After d = c:" << endl;
showArray(c);
showArray(d);

return 0;
}

数组补充

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
using namespace std;
/*
补充:数组的定义和使用
1,一维数组的定义和使用
2,二维数组的定义和使用
3,函数中的数组参数
*/


void showArray(int arr[], int len)
{
for(int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

void showMatrix(int mat[][3], int rowNum)
{
for(int i = 0; i < rowNum; i++)
{
for(int j = 0; j < 3; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}

int main()
{
int a[3] = {2, 4, 6};
showArray(a, 3);

cout << "-----------" << endl;

int m[3][3] = {1,2,3,4};
showMatrix(m,3);

return 0;
}

/*
可以用矩阵/行列式/数据表的概念来理解二维数组

二维数组定义:数据类型 数组名[第一维长度][第二维长度];
数据类型 数组名[行数][列数];
二维数组元素的表示方式: 数组名[元素第一维下标][元素第二维下标]
数组名[元素行下标][元素列下标]
对于一个M行*N列的二维数组来说,
数组元素的行下标范围[0,M-1],列下标范围[0,N-1]
例如:正确格式:m[1][2] , 错误格式:m[1,2]
对数组访问:使用穷举/列举方式对数组元素逐一访问,双重循环访问
数组元素的初始化
*/

int main6()
{
//int mat[2][3] = { {1,2,3} , {4,5,6} };
//int mat[2][3] = { {1} , {4,5} };
//int mat[2][3] = { 1,2,3,4,5,6 };
//int mat[2][3] = { 1,2,3,4 };
//int mat[2][3] = { 1 };
int mat[2][3] = { 0 };

cout << "The 2*3 matrix: " <<endl;

for(int i = 0; i < 2; i++)//外循环:遍历每一行 , 行下标范围:[0,1]
{
for(int j = 0; j < 3; j++)//内循环:遍历当前行的每一列,列下表范围:[0,2]
{
cout << mat[i][j] <<" ";
}
cout << endl;
}
return 0;
}

int main5()
{
int mat[2][3];

cout << "Enter a 2*3 matrix: " <<endl;

for(int i = 0; i < 2; i++)//外循环:遍历每一行 , 行下标范围:[0,1]
{
for(int j = 0; j < 3; j++)//内循环:遍历当前行的每一列,列下表范围:[0,2]
{
cin >> mat[i][j];
}
}

int sum = 0;
for(int i = 0; i < 2; i++)//外循环:遍历每一行 , 行下标范围:[0,1]
{
for(int j = 0; j < 3; j++)//内循环:遍历当前行的每一列,列下表范围:[0,2]
{
sum += mat[i][j];
}
}

cout << "The matrix is : " << endl;
for(int i = 0; i < 2; i++)//外循环:遍历每一行 , 行下标范围:[0,1]
{
for(int j = 0; j < 3; j++)//内循环:遍历当前行的每一列,列下表范围:[0,2]
{
cout << mat[i][j] <<" ";
}
cout << endl;
}

cout <<"Their summary is " << sum <<endl;

return 0;
}


int main4()
{
int mat[2][3];//定义一个2行3列的二维数组,总共6个元素,每个元素都是一个整型变量
cout << "Enter a 2*3 matrix: " <<endl;
cin >>mat[0][0] >> mat[0][1] >> mat[0][2];
cin >>mat[1][0] >> mat[1][1] >> mat[1][2];

int sum = mat[0][0] + mat[0][1] + mat[0][2];

cout << "mat[0][0] = " << mat[0][0] << endl;

return 0;
}

/*一维数组定义:数据类型 数组名[数组长度];
数组元素的表示方式: 数组名[元素下标]
元素下标范围:0 ~ N-1
对数组访问:使用穷举/列举方式对数组元素逐一访问,循环访问
数组元素的初始化
*/

int main3()
{
//int a[3] = {1,2,3};
//int a[3] = {1,2};
//int a[3] = {1};
int a[3] = {0};

cout << "The elements of the array are ";
for(int i = 0; i < 3; i++)
{
cout << a[i] << " , ";
}
cout << endl;

return 0;
}

int main2()
{
int a[3];

cout << "Enter 3 numbers: ";

for(int i = 0; i < 3; i++)
{
cin >> a[i];
}

cout << "The elements of the array are ";
for(int i = 0; i < 3; i++)
{
cout << a[i] << " , ";
}
cout << endl;

int sum = 0;
for(int i = 0; i < 3; i++)
{
sum += a[i];
}
cout << "Their summary is " << sum << endl;


return 0;
}

int main1()
{
int a[3];//定义了一个包含3个元素的整型数组,每个数组元素都是一个整型变量
cout << "Enter 3 numbers: ";
//cin >> a >> b >> c;
cin >> a[0] >> a[1] >> a[2];
cout << "The elements of the array are ";
cout << a[0] << " , " << a[1] << " , " <<a[2] << endl;
cout << "Their summary is " << a[0] + a[1] + a[2] << endl;


return 0;
}

string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <cstring> //for strlen(), strcpy(), strcat(), strcmp(), ...
#include <string> //for string class
#include <vector> //for vector template class
using namespace std;

//集训
string s="I love you";
if(s.find("love")!=-1)
{
cout<<s.find("love")<<endl;
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
cout<<s.substr(2,4)<<endl;//从下标为2开始往后取4个字符



//字符串类string: <string>, 第10章和第21章
//常用的字符串(字符数组和string类)输入输出方式
//常用的三种输入方式:逐字符输入, 整体输入/连续字符串输入, 整行输入
//常用的两种输出方式:逐字符输出, 整体输出

int main()
{
string str = "";
cout << str.size() << endl;

cout << "Enter a string : " << endl;

//第1种输入方式:逐字符输入(前提是已存在至少1个元素)
/*
str.resize(10);
for(int i = 0; i < str.size(); i++)
{
cin >> str[i];//第1种方式:逐字符输入,输入10个有效字符(分隔符之外的其他可见字符)
}
*/

//第2种输入方式:整体输入/连续字符串输入
//cin >> str; //整体输入/连续字符串输入,从键盘输入流中提取第一个连续字符串保存到字符串对象str中

//第3种方式:整行输入
//cin.getline(s, 20)//对于字符数组s的整行输入
getline(cin, str);//整行输入,以换行符/回车键结束,中间可以有空格或制表符/Tab键,不受长度限制

//整体输出
cout << "str = " << str << endl;

//逐字符输出
for(int i = 0; i < str.size(); i++)
{
cout << i << " : " << str[i] << " , " << int(str[i]) << endl;
}
return 0;
}

int main4()
{
string str = "abc";
str[0]=1;
cout << "str.size() = " << str.size() << endl;//strlen()
cout << "str.length() = " << str.length() << endl;//strlen()

cout << "str = " << str << endl;

string str2 = "123";

str = str2;//字符串复制/赋值, strcpy()
cout << "str = " << str << endl;

str = str + "456";//字符串连接/拼接, strcat()
cout << "str = " << str << endl;

str = "000" + str + "789";
cout << "str = " << str << endl;

str = "abc";
if(str == "abd")//字符串比较, strcmp(), (数值大小)关系运算符, >, >=, <, <=, ==, !=
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}

return 0;
}

int main3()
{
string str = "abc";
cout << "str.size() = " << str.size() << endl;
cout << "str.length() = " << str.length() << endl;

str.push_back('a');
cout << "str.size() = " << str.size() << endl;

str.resize(10);
cout << "str.size() = " << str.size() << endl;

return 0;
}

int main2()
{
//string str;
vector<int> a;
cout << "a.size() = " << a.size() << endl;

a.push_back(1);
cout << "a.size() = " << a.size() << endl;

a.resize(10);
cout << "a.size() = " << a.size() << endl;

return 0;
}

int main1()
{
int b[20] = {1, 2, 3};
//char s[20] = {'a', 'b', 'c'};
//char s[20] = {"abc"};
char s[20] = "abc";

cout << "s = " << s << endl;//整体输出

cout << "strlen = " << strlen(s) << endl;

strcpy(s, "123");
cout << "s = " << s << endl;//整体输出

strcat(s, "456");
cout << "s = " << s << endl;//整体输出

cout << strcmp(s, "123") << endl;

return 0;

cout << "Enter a string: ";
//for(int i = 0; i < 20; i++)
//{
// cin >> s[i];//第1种方式:逐字符输入,输入20个有效字符(分隔符之外的其他可见字符)
//}

//cin >> s;//第2种方式:整体输入/连续字符串输入,从键盘输入流中提取第一个连续字符串保存到字符数组s中

cin.getline(s, 20);//第3种方式:整行输入,以换行符/回车键结束,中间可以有空格或制表符/Tab键

cout << "s = " << s << endl;//整体输出
//cout << "b = " << b << endl;

for(int i = 0; i < 20; i++)//逐字符输出
{
//cout << b[i] << " ";
cout << s[i] << " , " << int(s[i]) << endl;
}


return 0;
}

指针&字符数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <iostream>
#include <vector>
#include <cstring> //for strlen(), strcpy(), strcat(), strcmp(), ...
using namespace std;

/*
指针和数组,动态数组(第10章)
字符指针和字符数组(第22章)
*/

//字符数组:可以整体输入和整体输出

//'0'-'9': 48-57
//'A'-'Z': 65-90
//'a'-'z': 97-122

//index: 0 1 2 ... N-3 N-2 N-1

int main()
{
char str[20] = "";
cout << "Enter a string less than 20 chars: ";
cin >> str;
int len = strlen(str);

int n1 = 0, n2 = 0, n3 = 0;
char c;
for(int i = 0; i < len ; i++)
{
c = str[i];
if(c >= '0' && c <= '9')
{
n1++;
}
else if(c >= 'A' && c <= 'Z')
{
n2++;
}
else if(c >= 'a' && c <= 'z')
{
n3++;
}
}

cout << "Numbers : " << n1 << endl;
cout << "Upcase : " << n2 << endl;
cout << "Lowercase: " << n3 << endl;

return 0;
}

int main12()
{
char str[20] = "";
cout << "Enter a string less than 20 chars: ";
cin >> str;
int len = strlen(str);

int flag = 1;//1-same, 0-not same

for(int i = 0; i < len / 2; i++)
{
if(str[i] != str[len-1-i])
{
flag = 0;
break;
}
}

cout << (flag==1 ? "Yes" : "No") << endl;

return 0;
}

int main11()
{
char str[20] = "";
cout << "Enter a string less than 20 chars: ";
cin >> str;
int len = strlen(str);

char str2[20] = "";
for(int i = 0; i < len; i++)
{
str2[i] = str[len-1-i];
}

int flag = 1;//1-same, 0-not same
for(int i = 0; i < len; i++)
{
if(str[i] != str2[i])
{
flag = 0;
break;
}
}

if(flag == 1)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}

return 0;
}


int main8()
{
char s1[10] = "abc";
char s2[10] = "123";

cout << strcmp(s1, s2) << endl;//string compare: 1-大于, -1-小于, 0-等于
cout << strcmp(s1, "abc") << endl;
cout << strcmp(s1, "abd") << endl;


cout << "-----" << endl;

cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;

strcpy(s1, s2);//string copy, 字符串复制
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;

strcat(s1, s2);//string catenate, 字符串连接
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;

return 0;
}

int main7()
{
char str[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\0', 0};
cout << "strlen(): " << strlen(str) << endl;

for(int i = 0; i < strlen(str); i++)
{
cout << str[i];
}
return 0;
}

int main6()
{
int a[10] = {1,2,3};
//char str[10] = {'a', 'b', 'c', '\0'};//后面7个元素默认初始化为0
//char str[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 0};
//char str[10] = {"09AZaz"};

//char str[10] = "09AZaz";
char str[10] = {'a', 'b', '\0', 'd', 'e', 'f', 'g', 'h', '\0', 0};
cout << "str = " << str << "."<< endl;
for(int i = 0; i < 10 ; i++)
{
cout << i << " : " << str[i] << " , " << int(str[i]) << endl;
}
return 0;
}

int main5()
{
char arr[10];
cout << "Enter a string: ";
cin >> arr;//整体输入:从键盘输入流中提取第一个连续字符串(不包含分隔符)
cout << "The string is: ";
cout << arr;//整体输出:从第一个字符向后依次输出,一直到结束符('\0')为止
return 0;
}

int main4()
{
char a[10];
cout << "Enter a string less than 10 charactors: ";
for(int i = 0; i < 10; i++)
{
cin >> a[i];//不包含分隔符(空格,制表符/Tab键,换行符/回车键)
}
cout << "The string is : ";
for(int i = 0; i < 10 ; i++)
{
cout << a[i] << " ";
}
cout << endl;
/*
char c;
cout << "Enter a charactor: ";
cin >> c;
cout << "The entered charactor is : " << c << endl;
//cout << "Hello world!" << endl;
//cout << "H" << 'H' << endl;
*/
return 0;
}

//数组名就是指针
//动态数组:new:申请内存空间;delete:释放内存空间
//new和delete组合使用, new[]和delete[]组合使用

void showArray(int arr[], int len)
{
for(int i = 0; i < len; i++)
{
cout << i << " : " << arr[i] << " , " << &(arr[i]) << endl;
}
cout << endl;
}

void showArray2(int *arr, int len)
{
for(int i = 0; i < len; i++)
{
cout << i << " : " << arr[i] << " , " << &(arr[i]) << endl;
}
cout << endl;
}

int main3()
{
int *p;
for(int i = 1; i <= 10000; i++)
{
cout << "i = " << i << endl;
p = new int[1024*1024*100];//申请 1024*1024*100*4=400MB内存空间
//process
delete[] p;
}
return 0;
}

int main222()
{
int n;
cout << "Enter n: ";
cin >> n;
cout << "Enter " << n << " integers: ";

int *p = new int[n];//申请动态数组

for(int i = 0; i < n; i++)
{
cin >> p[i];
}

int maxData = p[0];
int index = 0;
for(int i = 0; i < n; i++)
{
if(p[i] > maxData)
{
maxData = p[i];
index = i;
}
}
cout << "max data = " << maxData << " , index = " << index;

delete[] p;//释放动态数组内存空间,避免内存泄漏

return 0;
}

int main2()
{
int n;
cout << "Enter n: ";
cin >> n;
cout << "Enter " << n << " integers: ";

//int *p = new int[n];//申请动态数组
vector<int> p(n);

for(int i = 0; i < n; i++)
{
cin >> p[i];
}

int maxData = p[0];
int index = 0;
for(int i = 0; i < n; i++)
{
if(p[i] > maxData)
{
maxData = p[i];
index = i;
}
}
cout << "max data = " << maxData << " , index = " << index;

//delete[] p;//释放动态数组内存空间,避免内存泄漏

return 0;
}

int main1()
{
int a[3] = {1,2,3};
int b[3] = {4,5,6};

cout << "a = " << a << endl;
showArray(a,3);

cout << "b = " << b << endl;
showArray(b,3);

cout << "------" << endl;

int *p;
p = a;
showArray(p,3);

p = b;
showArray(p,3);

//a = b;//error


return 0;
}

指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
using namespace std;

/*
第8章 指针的定义和使用
指针和引用
指针和函数:函数的三种传参方式
指针和数组

指针:pointer, 用于保存内存地址的变量

引用:reference, 是给一个已有变量起一个别名(第二个名字), 引用本身不用再次分配内存空间
对引用的所有操作等价于对原始变量本身直接操作
*/

//函数的三种传参方式:传值,传地址,传引用

void test1(int a)//传值/按值传递
{
cout << "In test1(), begin: a = " << a << endl;
a = 2;
cout << "In test1(), end: a = " << a << endl;
}

void test2(int *a)//传地址/按地址传递
{
cout << "In test2(), begin: a = " << a << endl;
cout << "In test2(), begin: *a = " << *a << endl;
*a = 3;
cout << "In test2(), end: a = " << a << endl;
cout << "In test2(), end: *a = " << *a << endl;
}

void test3(int &a)//传引用/按引用传递
{
cout << "In test3(), begin: a = " << a << endl;
a = 4;
cout << "In test3(), end: a = " << a << endl;
}

int main()
{
int x = 1;
cout << "In main(), before test1(): x = " << x << endl;
test1(x);//传值/按值传递
cout << "In main(), after test1(): x = " << x << endl;

cout << "------------" << endl;
cout << "In main(), &x = " << &x << endl;
cout << "In main(), before test2(): x = " << x << endl;
test2(&x);//传地址/按地址传递
cout << "In main(), after test2(): x = " << x << endl;

cout << "------------" << endl;
cout << "In main(), before test3(): x = " << x << endl;
test3(x);//传引用/按引用传递
cout << "In main(), after test3(): x = " << x << endl;

return 0;
}

int main2()
{
int a = 12;
int &b = a;//定义变量a的一个引用为b,给变量a起一个别名叫b

cout << "sizeof(a) = " << sizeof(a) << endl;//变量a的内存大小
cout << "a = " << a << endl;//变量a的值/取值/数值
cout << "&a = " << &a << endl;//变量a的内存地址

cout << "sizeof(b) = " << sizeof(b) << endl;//变量b的内存大小
cout << "b = " << b << endl;//变量b的值/取值/数值
cout << "&b = " << &b << endl;//变量b的内存地址

cout << "--------" << endl;

b = 1;//等价于:a = 1;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

a = 2;//等价于:a = 1;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

return 0;
}

int main1()
{
//int a;
//a = 12;
int a = 12;
cout << "sizeof(int) = " << sizeof(int) << endl;
cout << "sizeof(a) = " << sizeof(a) << endl;//变量a的内存大小
cout << "a = " << a << endl;//变量a的值/取值/数值
cout << "&a = " << &a << endl;//变量a的内存地址

int *p;//定义一个指向整型变量的整型指针变量
p = &a;//把变量a的地址保存到指针变量p的内存空间,也称为:p指向a
//int *p = &a;
cout << "p = " << p << endl;//指针变量p的值/取值/数值
cout << "sizeof(p) = " << sizeof(p) << endl;//指针变量p的内存大小
cout << "&p = " << &p << endl;//指针变量p的内存地址

cout << "*p = " << *p << endl; //*p: 指针p所指向的那个变量

*p = 1;//等价于:a = 1;

cout << "a = " << a << endl;
cout << "*p = " << *p << endl;

a = 2;//等价于:*p = a;

cout << "a = " << a << endl;
cout << "*p = " << *p << endl;

cout << "--------" << endl;

int b = 3;

cout << "sizeof(b) = " << sizeof(b) << endl;//变量b的内存大小
cout << "b = " << b << endl;//变量b的值/取值/数值
cout << "&b = " << &b << endl;//变量b的内存地址

p = &b;//改变指针p的指向,现在p指向变量b

cout << "p = " << p << endl;//指针变量p的值/取值/数值
cout << "sizeof(p) = " << sizeof(p) << endl;//指针变量p的内存大小
cout << "&p = " << &p << endl;//指针变量p的内存地址
cout << "*p = " << *p << endl; //*p: 指针p所指向的那个变量

cout << "----------" << endl;

*p = 4; //此时等价于:b = 4;
cout << "*p = " << *p << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;


return 0;
}

cin.getline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string> 
#include <iostream>
using namespace std;

int main() {
char a;
int b;
float c;
string str;
cin>>a>>b>>c>>str;
cout<<a<<" "<<b<<" "<<c<<" "<<str<<endl;

string test;
getline(cin,test); //不阻塞
cout<<"test:"<<test<<endl;
return 0;
}
//从结果可以看出,cin>> 对缓冲区中的第一个换行符视而不见,采取的措施是忽略清除,继续阻塞等待缓冲区有效数据的到来。但是,getline() 读取数据时,并非像 cin>> 那样忽略第一个换行符,getline() 发现 cin 的缓冲区中有一个残留的换行符,不阻塞请求键盘输入,直接读取,送入目标字符串后,因为读取的内容为空,所以程序中的变量 test 为空串。

cin.get()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main() {
char a;
char b;
a=cin.get();
cin.get(b);
cout << a << b <<endl;
return 0;
}
//(1)从结果可以看出,cin.get() 从输入缓冲区读取单个字符时不忽略分隔符,直接将其读取,就出现了如上情况,将换行符读入变量 b,输出时换行两次,一次是变量 b,一次是 endl。
2)cin.get() 的返回值是 int 类型,成功则返回读取字符的 ASCII 码值。
3)cin.get(char var) 如果成功返回的是 cin 对象,因此支持链式操作,如cin.get(b).get(c)。

结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <iostream> //for cin, cout, endl, ...
#include <string> //for string class
using namespace std;

/*
结构:第22章,一种用户自定义的数据类型
结构的定义和使用
结构数组
结构指针
结构和函数
*/

struct Student
{
string id;//成员变量
string name;
int score;
};

void show1(Student a)//按值传递
{
cout << "---------" << endl;
cout << "In show1(int a):" << endl;
cout << "a.id = " << a.id << endl;
cout << "a.name = " << a.name << endl;
cout << "a.score = " << a.score << endl;
}

void show2(Student *a)//按指针传递
{
cout << "---------" << endl;
cout << "In show2(int *a):" << endl;

cout << "(*a).id = " << (*a).id << endl;//(*p)等价于a
cout << "(*a).name = " << (*a).name << endl;
cout << "(*a).score = " << (*a).score << endl;

cout << "a->id = " << a->id << endl;//(*p)等价于a
cout << "a->name = " << a->name << endl;
cout << "a->score = " << a->score << endl;
}

void show3(Student &a)//按引用传递
{
cout << "---------" << endl;
cout << "In show3(int &a):" << endl;
cout << "a.id = " << a.id << endl;
cout << "a.name = " << a.name << endl;
cout << "a.score = " << a.score << endl;
}

int main()
{
Student b;
b.id = "2112080001";
b.name = "Zhang San";
b.score = 88;

show1(b);//按值传递
show2(&b);//按指针传递
show3(b);//按引用传递

Student * p;
p = &b;
show2(p);//按指针传递

return 0;
}

int main6()
{
Student a;
a.id = "2112080001";
a.name = "Zhang San";
a.score = 88;


return 0;
}

int main5()
{
Student a;
int b;

a.id = "2112080001";
a.name = "Zhang San";
a.score = 88;

cout << "a.id = " << a.id << endl;
cout << "a.name = " << a.name << endl;
cout << "a.score = " << a.score << endl;

cout << "-------" << endl;

Student * p;//结构指针
int *p2;//整型指针
//p2 = &a; //error
//p = &b; //error
p = &a;//结构指针p指向结构变量a

cout << "(*p).id = " << (*p).id << endl;//(*p)等价于a
cout << "(*p).name = " << (*p).name << endl;
cout << "(*p).score = " << (*p).score << endl;

cout << "p->id = " << p->id << endl;//(*p)等价于a
cout << "p->name = " << p->name << endl;
cout << "p->score = " << p->score << endl;


return 0;
}

int main4()
{
int a;
a = 12;
cout << "a = " << a << endl;

int *p;
p = &a;//指针p指向整型变量a
cout << "*p = " << *p << endl;//*p: 指针p所指向的那个变量(此处是a)

*p = 13;//此处等价于:a = 13;
cout << "*p = " << *p << endl;
cout << "a = " << a << endl;

return 0;
}

int main3()
{
Student a[3];//结构数组

for(int i = 0; i < 3; i++)
{
cout << "Enter the ID of student " << i+1 << ": ";
cin >> a[i].id;
cout << "Enter the name of student " << i+1 << ": ";
cin >> a[i].name;
cout << "Enter the score of student " << i+1 << ": ";
cin >> a[i].score;
}

for(int i = 0; i < 3; i++)
{
cout << "Student " << i+1 << " : ";
cout << a[i].id << " , " << a[i].name << " , " << a[i].score << endl;
}

return 0;
}

int main2()
{
Student a;//结构变量
Student b;
//Student a, b;

a.id = "2112080001";
a.name = "Zhang San";
a.score = 88;

cout << "a.id = " << a.id << endl;
cout << "a.name = " << a.name << endl;
cout << "a.score = " << a.score << endl;

cout << "-------" << endl;

b = a;//整体赋值/复制

cout << "b.id = " << b.id << endl;
cout << "b.name = " << b.name << endl;
cout << "b.score = " << b.score << endl;

cout << "-------" << endl;

cout << "Enter the ID of student B: ";
cin >> b.id;
cout << "Enter the name of student B: ";
cin >> b.name;
cout << "Enter the score of student B: ";
cin >> b.score;

cout << "b.id = " << b.id << endl;
cout << "b.name = " << b.name << endl;
cout << "b.score = " << b.score << endl;

return 0;
}

int main1()
{

//int a;//char, short, int, long, long long, float, double, long double
int a;
//int b;
//int a, b;
a = 12;
cout << "a = " << a << endl;

return 0;
}

位运算 几类常用的库函数 static_cast

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <iostream>
#include <iomanip> //input output manipulation
#include <cctype> //for isdigit(), isalpha(), isupper(), ...
#include <cstdlib> //for atoi(), atof(), atol(), itoa(), ...
using namespace std;

//位运算:第22章,按二进制位进行运算, &, |, ~, ^, <<, >>
//几类常用的库函数

//字符串相关类型转换函数 : <cstdlib>, c-style standard library
//atoi(), atof(), atol(), itoa(), ...

int main()
{
int num = 12;
cout << "num = " << num << endl;

char str[20] = "";
itoa(num, str, 10);//integer to array, (整数,字符数组,进制)
cout << "str(10) = " << str << endl;

itoa(num, str, 8);
cout << "str(8) = " << str << endl;

itoa(num, str, 16);
cout << "str(16) = " << str << endl;

itoa(num, str, 2);
cout << "str(2) = " << str << endl;

return 0;
}

int main4()
{
char str[20] = "a123.67asfd6789";
cout << "The string is " << str << endl;

int a = atoi(str);//array to integer
cout << "atoi(str) = " << a << endl;

float f = atof(str);//array to float
cout << "atof(str) = " << f << endl;
cout << fixed << setprecision(6);
cout << "atof(str) = " << f << endl;

return 0;
}

//字符相关库函数, <cctype> c-style charactor type operation
//isdigit(), isalpha(), isspace(), isupper(), islower(), toupper(), tolower(), ...


int main3()
{
char c;
cout << "Enter a charactor: ";
cin >> c;
cout << "The charactor is " << c << endl;
if( isdigit(c) )
{
cout << "It is a digit." << endl;
}
else
{
cout << "It is not a digit." << endl;
}
cout << "It is " << (isalpha(c)? "" : "not ") << " a English letter." << endl;
if( isspace(c) )
cout << "It is a space." << endl;
else
cout << "It is not a space." << endl;

cout << "It is " << (isupper(c)? "" : "not ") << " uppercase." << endl;
cout << "It is " << (islower(c)? "" : "not ") << " lowercase." << endl;

char b = toupper(c);
cout << "Its uppercase is " << b << endl;
cout << "Its uppercase is " << char(toupper(c)) << endl;
cout << "Its lowercase is " << tolower(c) << endl;

return 0;
}

//^: 按位异或(相异得1,相同得0)
//&: 按位取与(都1得1,有0得0)
//|: 按位取或(有1得1,都0得0)
//~: 按位取反(0变1,1变0)
//<<:向左移位(按位左移,右侧补0)
//>>:向右移位(按位右移,无符号:左侧补0;有符号:左侧补符号位)
/*
a = 2 : 0 0000000 00000000 00000000 00000010
b = 3 : 0 0000000 00000000 00000000 00000011
~ b : 1 1111111 11111111 11111111 11111100
a | b : 0 0000000 00000000 00000000 00000011
a & b : 0 0000000 00000000 00000000 00000010
a ^ b : 0 0000000 00000000 00000000 00000001
a >> b: 0 0000000 00000000 00000000 00000000
a << b: 0 0000000 00000000 00000000 00010000
*/

// a = 13 : 00000000 00000000 00000000 00001101
// MASK : 00000000 00000000 00000000 00000001//掩膜
//a & MASK : 00000000 00000000 00000000 00000001
//a & MASK : 00000000 00000000 00000000 00000000

void showBinary(int a)//输出逆序二进制形式
{
const int MASK = 1;//00000000 00000000 00000000 00000001
const int LEN = sizeof(int) * 8;
for(int i = 1; i <= LEN; i++)
{
cout << (a & MASK);
cout << (i % 8 == 0 ? " " : "");
a = ( a >> 1 );
}
cout << endl;
}

void showBinary2(int a)//输出正序二进制形式
{
const int MASK = 1;
const int LEN = sizeof(int) * 8;
int bin[LEN] = {0};
for(int i = 0; i < LEN; i++)
{
bin[i] = (a & MASK);
a = ( a >> 1 );
}
for(int i = LEN - 1; i >= 0; i--)
{
cout << bin[i];
cout << (i % 8 == 0 ? " " : "");
}
cout << endl;
}

void showBinary22(int a)//输出正序二进制形式
{
const int MASK = 1;
const int LEN = sizeof(int) * 8;
int bin[LEN] = {0};
for(int i = 0; i < LEN; i++)
{
bin[LEN-1-i] = (a & MASK);
a = ( a >> 1 );
}
for(int i = 0; i < LEN; i++)
{
cout << bin[i];
cout << (i % 8 == 7 ? " " : "");
}
cout << endl;
}

void showBinary3(int a)//输出正序二进制形式
{
const int MASK = 1;
const int LEN = sizeof(int) * 8;
for(int i = LEN - 1; i >= 0; i--)
{
cout << ( (a >> i) & MASK);
cout << (i % 8 == 0 ? " " : "");
}
cout << endl;
}

// a = 13 : 00000000 00000000 00000000 00001101
// MASK : 10000000 00000000 00000000 00000000//掩膜
//a & MASK : 00000000 00000000 00000000 00000000
//a & MASK : 10000000 00000000 00000000 00000000

void showBinary4(int a)//输出正序二进制形式
{
const int LEN = sizeof(int) * 8;
const int MASK = (1 << (LEN - 1)); //10000000 ... 00000000
for(int i = 0; i < LEN; i++)
{
cout << ( ( (a << i) & MASK) == 0 ? 0 : 1 );
cout << (i % 8 == 7 ? " " : "");
}
cout << endl;
}


int main2()
{
int a = 13;
cout << "a = " << a << endl;
showBinary(a);
showBinary2(a);
showBinary22(a);
showBinary3(a);
showBinary4(a);
//cout << "decimal : a = " << dec << a << endl;//十进制
//cout << "octal : a = " << oct << a << endl;//八进制
//cout << "hex : a = " << hex << a << endl;//十六进制

//cout << ( a << 1 ) << endl;
//cout << ( a >> 1 ) << endl;

return 0;
}

int main1()
{
int a = 2;
int b = 3;
cout << "a = " << a << " , b = " << b << endl;
cout << "a ^ b = " << ( a ^ b ) << endl;
cout << "a & b = " << ( a & b ) << endl;
cout << "a | b = " << ( a | b ) << endl;
cout << "~ b = " << ( ~ b ) << endl;
cout << "a << b = " << ( a << b ) << endl;
cout << "a >> b = " << ( a >> b ) << endl;
return 0;
}

1
2
3
4
float a;
cin>>a;
int b=static_cast<int>(a);//强转float为int
double c=static_cast<double>(a);//强转float为double

一元作用域分辨运算符的用途

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>//2112080162-张晓杰
#include <cstdlib>
#include <ctime>
using namespace std;
int a = 100;

int main()
{
int a = 50;
cout << "作用域运算符::可以用来解决局部变量与全局变量的重名问题,访问被屏蔽的全局变量";
cout << "例子:" << endl;
cout << "a: " << a << "------" << "::a : " << ::a;
return 0;
}

随机数另外一种

1
2
3
4
5
default_random_engine engine(static_cast<unsigned int>(time(0)));
uniform_int_distribution<unsigned int>randomInt(1, 6);//生成1-6的随机数
unsigned int die1 = randomInt(engine);
unsigned int die2 = randomInt(engine);
//比如[a,b]区间上随机,就用rand()%(b-a+1)+a

格式控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <iomanip> //for dec, oct, hex, fixed, ...
using namespace std;

//IO格式控制:第13章(13.6和13.7)
//<iomanip>: input output stream manipulator
//fixed, setprecision(), hex, dec, oct, ...

//格式控制两种设置方式:cout.setf(控制符), cout << 控制符
/*
浮点数精度:默认精度为6,可以通过setprecision()来设置新的浮点数精度
非定点数模式:从左侧第一个非零数值开始的连续有效数字个数
定点数模式:小数点后连续有效数字个数
取消设置/回复默认:cout.unsetf(控制符)
*/

/*
setw(): 设置显示宽度 (单次/一次性操作,仅仅对后面的第一个对象有效)
其他的格式控制符:大都是黏性操作(持久性/永久性操作,设置之后持续有效,直到新的设置为止)
left : 左对齐
right: 右对齐
默认对齐方式为右对齐
setfill(): 设置填充字符,默认填充字符是空格
*/

int main()
{
cout << setfill('*');
for(int i = 1; i <= 5; i ++)
{
cout << setw(i) << "" << endl;
}
return 0;
}

int main5()
{
for(int i = 1; i <= 5; i ++)
{
for(int j = 1; j <= i; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}

int main4()
{
cout << "12345678901234567890" << endl;
//cout << "abc\tabc\tabc\n";
cout << setw(10) << "abc" << setw(10) << "123" << endl;//默认对齐方式为右对齐
cout << left;
//cout.setf(ios_base::left);
cout << setw(10) << "abc" << setw(10) << "123" << endl;
cout << right;
cout << setfill('*');
//cout.setf(ios_base::right);
cout << setw(10) << "abc" << setw(10) << "123" << endl;

cout << left << setw(10) << "abc" << right << setw(10) << "123" << endl;
char c = '%';
cout << setfill(c);
cout << right << setw(10) << "abc" << left << setw(10) << "123" << endl;

return 0;
}

int main3()
{
double a = 3.1415926, b = a * 0.0001;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

cout << "------" << endl;
//cout << scientific;//设置科学计数法显示形式
cout.setf(ios_base::scientific);//设置科学计数法显示形式
cout << "a = " << a << endl;
cout << "b = " << b << endl;

cout << "------" << endl;
cout.unsetf(ios_base::scientific);//取消科学计数法显示形式
cout << "a = " << a << endl;
cout << "b = " << b << endl;

return 0;
}

int main2()
{
double a = 3.1415926, b = a * 0.0001;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

cout << "------" << endl;
//cout << fixed;
cout.setf(ios_base::fixed);
cout << "a = " << a << endl;
cout << "b = " << b << endl;

cout << "------" << endl;
cout << setprecision(3);
cout << "a = " << a << endl;
cout << "b = " << b << endl;

cout.unsetf(ios_base::fixed);//取消定点数显示

cout << "------" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

cout << "------" << endl;
cout << setprecision(6);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}

int main1()
{
int a = 12;
cout << "a = " << a << endl;
cout << "decimal: a = " << dec << a << endl;
cout << "octal: a = " << oct << a << endl;
cout << "hexadecimal/hex: a = " << hex << a << endl;
return 0;
}

类和对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <iostream>
#include <string> //for string class
#include <vector>
using namespace std;

/*
类和对象: 第3章
类和对象的定义和使用
对象数组
对象指针
对象和函数: 三种传参方式
*/

/*
成员的访问权限: public(公有成员), protected(保护成员), private(私有成员)
struct: 成员的访问权限默认为public: 可以在结构外直接访问
class: 成员的访问权限默认为private: 不可以在类外直接访问,但可以在类内访问
*/
//对象:类类型的变量称为对象
//成员:两种成员类型:成员变量,成员函数

class Cube
{
private:
int length;
int width;
int height;
int volume;

public:
void setLength(int len)
{
length = len;
}
int getLength()
{
return length;
}
void setWidth(int wid)
{
width = wid;
}
int getWidth()
{
return width;
}
void setHeight(int hei)
{
height = hei;
}
int getHeight()
{
return height;
}
void setPara(int len, int wid, int hei)
{
length = len, width = wid, height = hei;
}
int getVolume()
{
return length * width * height;
}
};

int main()
{
int n;
cout << "Enter the number of the cubes: ";
cin >> n;

//Cube list[n];
//Cube *list = new Cube[n];

vector<Cube> list(n);

int len, wid, hei;

for(int i = 0; i < n; i++)
{
cout << "Enter the length, width and height of the cube " << i+1 << " : ";
cin >> len >> wid >> hei;
//list[i].setLength(len);
//list[i].setWidth(wid);
//list[i].setHeight(hei);
list[i].setPara(len, wid, hei);
}

double sum = 0;
for(int i = 0; i < n; i++)
{
//sum += list[i].getLength() * list[i].getWidth() * list[i].getHeight();
sum += list[i].getVolume();
}
cout << "The average volume is " << sum / n << endl;

//delete[] list;

return 0;
}



class Student
{
public:
string id;
string name;
int score;
};

class Student2
{
private:
string id;
string name;
int score;

public:
void setID(string i)
{
id = i;
}

string getID()
{
return id;
}

void setName(string n)
{
name = n;
}

string getName()
{
return name;
}

void setScore(int s)
{
if(s < 0)
{
score = 0;
}
else if( s > 100)
{
score = 100;
}
else
{
score = s;
}
}

int getScore()
{
return score;
}

void setInfo(string i, string n, int s)
{
id = i;
name = n;
score = s;
}
};


int main2()
{
Student2 stu;

string id;
string name;
int score;

cout << "Input the ID, name and score of the student: ";
cin >> id >> name >> score;

/*
stu.setID(id);
stu.setName(name);
stu.setScore(score);
*/
stu.setInfo(id, name, score);

cout << "The student : ";
cout << stu.getID() << " , " << stu.getName() << " , " << stu.getScore() << endl;


return 0;
}



void inputStudent(Student & s, int i)
{
cout << "Input the ID, name and score of the student " << i + 1 << " : ";
cin >> s.id >> s.name >> s.score;
}

void modifyStudent(Student * s)
{
//type 1: (*pointer).member
if( (*s).score < 0 )
{
(*s).score = 0;
}

//type 2: pointer->member
if( s->score > 100 )
{
s->score = 100;
}
}

void outputStudent(Student s, int i)
{
cout << "The student " << i + 1 << " : ";
cout << s.id << " , " << s.name << " , " << s.score << endl;
}

int main1()
{
//input
Student stu[3];
for(int i = 0; i < 3; i++)
{
inputStudent(stu[i], i);
}

//process
Student * p;
for(int i = 0; i < 3; i++)
{
//modifyStudent( &(stu[i]) );//type 1 of pointer : &()
//p = &(stu[i]);//type 2
p = stu + i; //type 3
modifyStudent( p );
}

//output
for(int i = 0; i < 3; i++)
{
outputStudent(stu[i], i);
}

return 0;
}

构造析构和多文件结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string> //for string class
#include "Student.h"
#include "Cube.h"
using namespace std;

/*
构造析构和多文件结构:第9章,第3章
构造函数和析构函数:两种特殊的公有成员函数,都是自动调用,都没有返回类型
构造函数:在定义对象时自动调用,主要用于成员变量初始化,
可以重载,形参可有可无,函数名:类名
析构函数:在释放对象时自动调用,主要用于收尾工作(如释放动态指针内存空间)
只有一个,不能重载,没有形参,函数名:~类名
释放多个对象时,析构函数的调用顺序与构造函数相反(逆序析构)
*/

int main()
{
Student s1;//自动调用无参构造函数
Student s2("222", "Jerry", 88);//自动调用有参构造函数
Cube c1;//自动调用无参构造函数
Cube c2(22);//自动调用有参构造函数
return 0;//释放对象时,逆序释放,和构造 函数的调用顺序相反
}

int main2()
{
Student s[3];//定义一个包含3个对象的对象数组,给每个对象分配内存时都将自动调用一次构造函数
for(int i = 0; i < 3; i++)
{
s[i].setScore(i);
}
return 0;//程序结束,释放所有变量,释放每个对象时都将自动调用一次析构函数
}

int main1()
{
Student s;//定义对象时自动调用Student类的构造函数
//s.setID("111");
//s.setName("Tom");
//s.setScore(88);
cout << "The student: " << s.getID() << " , " << s.getName() << " , " << s.getScore() << endl;
return 0;//main()函数结束,释放所有变量,释放对象s时将自动调用Student类的析构函数
}

头文件cube

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

//第二种方式: 对于成员函数,类内声明,类外定义
//类外定义时,需要在函数名之前加前缀"类名::"
//"::": 域操作符,表示所属关系
class Cube
{
public:
Cube();
Cube(int aa);
~Cube();
void setA(int aa);
int getA();

private:
int a;
};

Cube::Cube()
{
a = 0;
cout << "This is constructor of class Cube. a = " << a << endl;//only for test
}
Cube::Cube(int aa)
{
a = aa;
cout << "This is constructor of class Cube. a = " << a << endl;//only for test
}
Cube::~Cube()
{
cout << "This is destructor of class Cube. a = " << a << endl;//only for test
}
void Cube::setA(int aa)
{
a = aa;
}
int Cube::getA()
{
return a;
}

头文件Student

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <string>
#include <iostream>
using namespace std;

//第一种方式: 对于成员函数,类内定义
class Student
{
public:
Student()//无参构造函数
{
id = "000";
name = "NONE";
score = 0;
cout << "This is constructor of class Student. score = " << score << endl;//only for test
}
Student(string i, string n, int s)//有参构造函数
{
id = i;
name = n;
score = s;
cout << "This is constructor of class Student. score = " << score << endl;//only for test
}

~Student()//析构函数
{
cout << "This is destructor of class Student. score = " << score << endl;//only for test
}

void setID(string i)
{
id = i;
}
std::string getID()
{
return id;
}
void setName(string n)
{
name = n;
}
string getName()
{
return name;
}
void setScore(int s)
{
score = s;
}
int getScore()
{
return score;
}
private:
string id;
string name;
int score;
};

运算符重载和文件处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <string>
#include <fstream> //file stream, including ifstream, ofstream, fstream
using namespace std;

//运算符重载和文件处理:第10章和第14章

/*
友元:(第9章)是已有类的"朋友",可以在友元内访问类的所有成员
主要有友元函数和友元类两种类型。

运算符重载:用友元函数方式进行重载,使用"operator 运算符"作为函数名

写文件时,文件打开模式:
ios::out, (output), 覆盖写入模式, 清空原有内容,从头写入新内容
ios::app, (append), 追加写入模式, 保留原有内容,尾部写入新内容
如果文件存在,就打开文件,否则,如果文件不存在,则创建并打开一个空白文件

读文件时,文件打开模式;
ios::in, (input), 以只读方式直接打开文件 ,只能读取,不能写入

*/

class Square
{
friend void show(Square & s);//友元函数方式一:类内声明,类外定义
friend void show2(Square & s)//友元函数方式二:类内定义
{
cout << "s.side = " << s.side << endl;
}
friend istream& operator >> (istream& inputStream, Square& sq)//类内定义
{
inputStream >> sq.side;
return inputStream;
}
friend ostream& operator << (ostream& outputStream, Square& sq);//类内声明,类外定义

public:
void setSide(int s);
int getSide();
void draw();

private:
int side;
};

int main()
{
//char fileName[200] = "abc.txt";
string fileName = "abc.txt";

ofstream fout(fileName, ios::app);
//if(fout.fail())
if(!fout)
{
cout << "Failed to open the file " << fileName << endl;
}
else
{
fout << "Hello world!" << endl;
cout << "Success to write the file " << fileName << endl;
}
fout.close();

char c;
char str[200];

ifstream fin;
fin.open(fileName, ios::in);

if(fin.fail())
//if(!fin)
{
cout << "Failed to open the file " << fileName << endl;
}
else
{
cout << "Success to read the file " << fileName << endl;
cout << "The content of the file is: " << endl;
/*方式一:逐字符读取
//while(fin >> c)//逐字符读取(过滤掉所有分隔符:空格/制表符/换行符)
while(fin.get(c))//逐字符读取(包含所有分隔符:空格/制表符/换行符)
{
cout << c;
}
*/
/*方式二:逐单词读取
while( fin >> str )
{
cout << str << endl;
}
*/
//方式三:逐行读取
while( fin.getline(str, 200) )
{
cout << str << endl;
}
}

fin.close();
return 0;
}


int main4()
{
int a;
//cin >> a;//console input, 标准/控制台/键盘输入

char fileName[200] = "in.txt";

ifstream fin(fileName); //默认打开模式: ios::in(只读打开模式)

if(fin.fail())
{
cout << "Failed to open the file " << fileName << endl;
}
else
{
cout << "Success to read the file " << fileName << endl;
/*
fin >> a;
cout << "a = " << a << endl;
fin >> a;
cout << "a = " << a << endl;
fin >> a;
cout << "a = " << a << endl;
*/
while( fin >> a )//流状态:如果成功读取则返回true,否则返回false
{
cout << "a = " << a << endl;
}
}

fin.close();

return 0;
}

int main3()
{
//cout << "Hello world!" << endl;//console output,标准/控制台/屏幕输出
//ofstream fout;
//fout.open("out.txt");//文件的默认打开模式:ios::out(覆盖写入模式)
//fout.open("out.txt", ios::out);//制定打开模式为ios::out(覆盖写入模式)
//fout.open("out.txt", ios::app);//制定打开模式为ios::app(追加写入模式)

//string fileName = "out.txt";
char fileName[200] = "b/out.txt";
//char fileName[200] = "out.txt";

ofstream fout(fileName, ios::app);

if(fout.fail())
{
cout << "Failed to open the file " << fileName << endl;
}
else
{
fout << "Hello world!" << endl;
cout << "Success to write the file " << fileName << endl;
}

fout.close();

return 0;
}


int main2()
{
Square sq;
cin >> sq;
cout << sq;
return 0;
}

int main1()
{
cout << "Input the side length of the square: ";
int side;
cin >> side;

Square sq;
sq.setSide(side);
sq.draw();

return 0;
}

void Square::setSide(int s)
{
side = s;
}

int Square::getSide()
{
return side;
}

void Square::draw()
{
for(int i = 1; i <= side; i++)
{
for(int j = 1; j <= side; j++)
{
cout << "*";
}
cout << endl;
}
}

void show(Square & s)
{
cout << "s.side = " << s.side << endl;
}

ostream& operator << (ostream& outputStream, Square& sq)
{
for(int i = 1; i <= sq.side; i++)
{
for(int j = 1; j <= sq.side; j++)
{
outputStream << "*";
}
outputStream << endl;
}
return outputStream;
}

继承和多态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <iostream>
using namespace std;

const double PI = 3.1415926;

/*
继承和多态:第11章,第12章
继承:基于已有类来定义新类,已有类:基类/父类,新类:派生类/子类
子类除了具有基类的所有成员(包括成员变量和成员函数)之外,
还可以增加自己特有的成员(包括成员变量和成员函数)

在定义子类对象时, 先自动调用基类的构造函数,然后再自动调用子类本身的构造函数
在释放子类对象时, 先自动调用子类本身的析构函数,然后再自动调用基类的析构函数(逆序析构)

继承方式:public, protected, private
结构类型struct默认是public继承
类类型class默认是private继承

多态:在具有继承关系的多个对象中,通过基类指针(或基类引用)来调用不同子类中的同名成员函数,
自动根据不同子类的类型,来实现不同的功能(呈现不同的形态)

通常使用虚函数(virtual)来实现多态
*/

class Shape
{
public:
Shape();
~Shape();
void setPerimeter(double p);
double getPerimeter();
void setArea(double a);
double getArea();
virtual void draw();

private:
double perimeter;
double area;
};

class Circle : public Shape
{
public:
Circle();
~Circle();
void setRadius(double r);
double getRadius();
virtual void draw();

private:
double radius;
};

class Rectangle : public Shape
{
public:
Rectangle();
~Rectangle();
void setLength(double len);
double getLength();
void setWidth(double wid);
double getWidth();
void setLenWid(double len, double wid);
virtual void draw();

private:
double length;
double width;
};

void drawShape(Shape *pShape)
{
pShape->draw();
}

void drawShape(Shape & s)
{
s.draw();
}

int main()
{
Shape s;
Circle c;
Rectangle r;

cout << "-------------------------" << endl;

Shape *pShape;

pShape = &s;
pShape->draw();

pShape = &c;
pShape->draw();

pShape = &r;
pShape->draw();

cout << "-------------------------" << endl;

drawShape(&s);
drawShape(&c);
drawShape(&r);

cout << "-------------------------" << endl;

drawShape(s);
drawShape(c);
drawShape(r);

cout << "-------------------------" << endl;

return 0;
}

int main3()
{
Shape s;
Circle c;
Rectangle r;

cout << "-------------------------" << endl;

Shape * pShape;
pShape = &s;
(*pShape).setArea(12);
cout << "pShape->getArea() : " << pShape->getArea() << endl;

cout << "-------------------------" << endl;

Circle * pCircle;
pCircle = &c;
(*pCircle).setRadius(2);
cout << "pCircle->getArea() : " << pCircle->getArea() << endl;

cout << "-------------------------" << endl;

Rectangle * pRectangle;
pRectangle = &r;
(*pRectangle).setLenWid(5, 4);
cout << "pRectangle->getArea() : " << pRectangle->getArea() << endl;

cout << "-------------------------" << endl;

return 0;
}

int main2()
{
Shape s;

cout << "-------------------------" << endl;

Circle c;

cout << "-------------------------" << endl;

Rectangle r;

cout << "-------------------------" << endl;

return 0;
}

int main1()
{
Shape s;
cout << "sizeof(Shape) = " << sizeof(Shape) << endl;
cout << "sizeof(s) = " << sizeof(s) << endl;
s.setArea(12);
//s.area = 12;//error
cout << "Shape. s.getArea() = " << s.getArea() << endl;

cout << "-------------------------" << endl;

Circle c;
cout << "sizeof(Circle) = " << sizeof(Circle) << endl;
cout << "sizeof(c) = " << sizeof(c) << endl;
//c.setArea(13);
c.setRadius(2);
cout << "Circle. c.getArea() = " << c.getArea() << endl;

cout << "-------------------------" << endl;

Rectangle r;
cout << "sizeof(Rectangle) = " << sizeof(Rectangle) << endl;
cout << "sizeof(r) = " << sizeof(r) << endl;
//r.setArea(14);
r.setLenWid(5, 4);
cout << "Rectangle. r.getArea() = " << r.getArea() << endl;

cout << "-------------------------" << endl;

/*
int a;
double b;
cout << "sizeof(int) = " << sizeof(int) << endl;
cout << "sizeof(a) = " << sizeof(a) << endl;
cout << "sizeof(double) = " << sizeof(double) << endl;
cout << "sizeof(b) = " << sizeof(b) << endl;
*/
return 0;
}

Shape::Shape()
{
perimeter = area = 0;
cout << "Shape constructor . area = " << area << endl;//only for test
}

Shape::~Shape()
{
cout << "Shape destructor . area = " << area << endl;//only for test
}

void Shape::setPerimeter(double p)
{
perimeter = p;
}

double Shape::getPerimeter()
{
return perimeter;
}

void Shape::setArea(double a)
{
area = a;
}

double Shape::getArea()
{
return area;
}

void Shape::draw()
{
cout << "Shape::draw() : draw a shape here" << endl;
}

Circle::Circle()
{
radius = 0;
cout << "Circle constructor . area = " << getArea() << endl;//only for test
}

Circle::~Circle()
{
cout << "Circle destructor . area = " << getArea() << endl;//only for test
}

void Circle::setRadius(double r)
{
radius = r;
//perimeter = 2 * PI * radius;//error
//area = PI * radius * radius;//error
setPerimeter(2 * PI * radius);
setArea(PI * radius * radius);
}

double Circle::getRadius()
{
return radius;
}

void Circle::draw()
{
cout << "Circle::draw() : draw a circle here" << endl;
}

Rectangle::Rectangle()
{
length = width = 0;
cout << "Rectangle constructor . area = " << getArea() << endl;//only for test
}

Rectangle::~Rectangle()
{
cout << "Rectangle destructor . area = " << getArea() << endl;//only for test
}

void Rectangle::setLength(double len)
{
length = len;
}

double Rectangle::getLength()
{
return length;
}

void Rectangle::setWidth(double wid)
{
width = wid;
}

double Rectangle::getWidth()
{
return width;
}

void Rectangle::setLenWid(double len, double wid)
{
length = len;
width = wid;
setPerimeter( (length + width) * 2);
setArea(length * width);
}

void Rectangle::draw()
{
cout << "Rectangle::draw() : draw a rectangle here" << endl;
}

[【c++】size_t 和 size_type的区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
为了使自己的程序有很好的移植性,c++程序员应该尽量使用size_t和size_type而不是int, unsigned

1. size_t是全局定义的类型;size_type是STL类中定义的类型属性,用以保存任意string和vector类对象的长度

2. string::size_type 制类型一般就是unsigned int, 但是不同机器环境长度可能不同 win32 和win64上长度差别;size_type一般也是unsigned int
3. 使用的时候可以参考:
string::size_type a =123;
vector<int>size_type b=234;
size_t b=456;
4. size_t 使用的时候头文件需要 <cstddef> ;size_type 使用的时候需要<string>或者<vector>
5. sizeof(string::size_type)
sizeof(vector<bool>::size_type)
sizeof(vector<char>::size_type)
sizeof(size_t)
上述长度均相等,长度为win32:4 win64:8
6. 二者联系:在用下标访问元素时,vector使用vector::size_type作为下标类型,而数组下标的正确类型则是size_t

nops的使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*npos可以表示string的结束位子,是string::type_size 类型的,也就是find()返回的类型。find函数在找不到指定值得情况下会返回string::npos。举例如下(计算字符串中含有的不同字符的个数):*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
string b;
getline(cin,b);
int count=0;
for(int i=0;i<=127;i++)
if(b.find(i)!=string::npos)
count++;
cout<<count;
}

string substr

1
2
3
4
5
6
7
8
9
10
11
12
13
// string::substr
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}

string强转const char*

1
2
3
string str1 = "asdf";
string str2 = "qwer";
strcmp(str1.c_str(), str2.c_str());要用c_str()

printf

(143条消息) printf()函数详解_望天际的博客-CSDN博客_printf函数

集训

时间复杂度

1
2
3
4
5
6
7
8
9
10
11
12
int n;
int s=0;
cin>>n;
for(int i = 2;i <= n; i++)
{
cout<<i<<"的倍数:";
for(int j=i;j<=n;j+=i)
{
cout<<j<<' ';
}
cout<<endl;
}时间复杂度是nlogn

image-20220620122641706

image-20220620122653126

image-20220620122658952

容器/迭代器

image-20220622185451388

image-20220622185726117

image-20220622185838964

pair

1
2
3
4
5
pair<int,int> a;
pair<int,int> b{3,4};
a={1,2};
pair<int,int>c(a);
cout<<a.first<<' '<<a.second<<endl;

tuple

1
2
3
tuple<int,int,int>a{1,12,3};
cout<<get<0>(a)<<endl; //输出1
vector<tuple<int,int,int>>类似结构体

auto(了解)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
array<int,6>a={1,2,3,4,5,6};
for(auto it = a.begin();it != a.end();++it)
{
cout<<*it<<endl;
}//遍历array的值

for(int i : a)
{
cout<<i<<" "<<;
}//遍历array的值

auto it2=a.begin();//这时指向1
cout<<*(it2+2)<<endl;//3
it2+=2;//这时指向3
cout<<*(it2-1)<<endl;//2
it2=a.end();//这时指向最后一位
cout<<*(it2-1)<<endl;//6,因为下标要-1

auto it=a.begin();
it+=2;
cout<<a[0]<<endl;//3
auto is = next(a.begin(),2);
cout<<*it<<endl;//3

auto it = prev(a.end(),2);
cout<< *it <<endl;