C++小tips

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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#include<iostream>
#include<string>
//#include "swap.h"

using namespace std;

//定义常量--第一种方式--宏常量
#define Pi 3.14

int numGuess() {
cout << "猜数字" << endl;
int uu = 0;
cin >> uu;
int num = rand() % 100;
while (uu != num)
{
if (uu > num) {
cout << "大了" << endl;
cin >> uu;
}
else
{
cout << "小了" << endl;
cin >> uu;
}
}
cout << "猜对了" << endl;
return uu;
}

int foruse(int u) {
float i = 1.000001;
for (; i < u; )
{
i *= i;
}
return i;
}


int base() {
//注释
cout << "Hello Shyee" << endl;
/*多行
注释*/

//创建变量a
int a = 10;
cout << "a= " << a << endl;
//定义常量的第二种方法--const
const int Day = 7;
cout << "Pi= " << Pi << endl;
cout << "Day= " << Day << endl;
//Day = 18; 修改常量会报错

short b = 65536;
cout << "b=" << b << endl;

//SizeOf关键字
//求数据类型站内存的大小

short num1 = 10;
cout << "short占内存大小为:" << sizeof(short) << endl;
cout << "short占内存大小为:" << sizeof(num1) << endl;

/*
short[短整型)
2字节
(-2^15~2^15 - 1)
int(整型)
4字节
(-2 ^ 31 ~2 ^ 31 - 1)
long(长整形
Windows为4字节, Linux为4字节(32位),8字节(64位)
(-2 ^ 31 ~2 ^ 31 - 1)
long long(长长整形
8字节
(-2 ^ 63 ~2 ^ 63 - 1)
*/

float f1 = 3.14f;//小数默认时双精度,所以后面加一个f
double f2 = 3.14;
cout << "f1:" << f1 << endl;
cout << "f2:" << f2 << endl;
//科学计数法
float f3 = 3e2;//e:10^2
cout << f3 << endl;
float f4 = 3e-2;//e:0.1^2
cout << "f4:" << f4 << endl;

//字符型
char ch = 'a';
cout << "ch占用内存空间:" << sizeof(ch) << endl;
cout << "a对应的ASCII码" << (int)ch << endl;

//c中的字符串
char str1[] = "C String";
//c++中的字符串

string str2 = "C++ String";

cout << str2 << endl;//使用时需要加一个头文件<string>

//布尔数据
bool x = true;
cout << "布尔类型的大小:" << sizeof(x) << endl;

//数据的输入
//整形
int u=0;
cout << "请给整型变量u进行赋值" << endl;

cin >> u;
cout << u << endl;

//浮点型
float f = 1.1f;
cout << "给float进行赋值" << endl;
//cin >> f;
cout << f << endl;

//字符串
string strl = "";
cout << "给String进行赋值" << endl;
//cin >> strl;
cout << "字符串strl:" << strl << endl;

//if
if (u < 45)
cout << "u小于45" << endl;
else
cout << "u不小于45" << endl;
switch (u)
{
case 10:
cout << "u此时被switch找到" << endl;
break;
default:
cout << "" << endl;
break;
}

while (u<20) {
u++;
}
cout <<"while让u自增到:"<< u << endl;
//三只小猪称体重
/*int piga = 0, pigb = 0, pigc = 0;
cout << "分别输入三个重量" << endl;
cin >> piga;
cin >> pigb;
cin >> pigc;
string fs = piga > pigb ? (piga > pigc ? "a最重" : "c最重") : (pigb > pigc ? "b最重" : "c最重");
cout << fs << endl;*/

//猜数字100以内
//u=numGuess();

f = foruse(u);
cout<<f<<endl;
system("pause");
return 0;
}

int arraylearn() {
//数组
int a[6];
char c[] = { 's','t','r','i','n','g' };
for (char i:c) {
cout << i;
}
cout << endl;
cout << sizeof(a[0]) << endl;
cout << sizeof(a)<<endl;
cout << sizeof(a)/ sizeof(a[0])<<"个元素" << endl;
cout << a << endl;
cout << &a[0] << endl;
cout << &a[1] << endl;
cout << (int)&a[1]-(int)&a[0] << endl;
//数组是常量
return 0;
}
int fivePigsWeght() {
int h = 0;
int a[5];
for (int i = 0; i < 5;i++) {
cin >> a[i];
}
for (int i:a) {
h=h < i ? i : h;
}
cout << h << endl;
return h;
}
//地址传递
void swap(int* a, int* b) {
int* c = 0;
c = a;
a = b;
b = c;

}
//引用传递
void swap01(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
//参数传递
void swap03(int a, int b) {
int temp = a;
a = b;
b = temp;
}
void swap02(int *p1,int *p2) {
int temp = *p1;
*p1 = *p2;
*p2 = *p1;
}
//调用swap(&a,&b)可实现地址传递的函数调用
//值传递不会修改实参
int arrayResive(int b[]) {
//int a[] = b;
int a[] = {1,2,3,4,5,6,7,8,9};
int f = 0;
int t = sizeof(a) / sizeof(a[0])-1;
for (int i:a) {
cout << i << endl;
}
while (f < t) {
swap(a[f], a[t]);
f++;
t--;
}
/*for (int i = 0;i< sizeof(a) / sizeof(a[0]);i++) {
if (f<t) {
swap(a[f], a[t]);
f++;
t--;
}
else {
break;
}
}*/
for (int i : a) {
cout << i << endl;
}
return 0;
}
int add(int a,int b) {
return a + b;
}
void helloworld() {
cout << "hello world" << endl;
}

int point() {
int a = 10;
int* p;
p = &a;
cout << "a的地址为:" << &a << endl;
cout << "p的值:" << p << endl;
//指针前加一个*代表解引用,找到指针所指向的内存的数据
*p = 100;
cout << a << endl;

return 0;
}

int pointSe() {
/*在32操作系统下占4个字节
64位系统占8个字节*/
int a = 10;
int* p = &a;
cout << sizeof(p) << endl;
return 0;
}
int nullpointAndRow() {
//空指针用于给指针变量进行初始化
//空指针不可以进行访问
int* p = NULL;
//*p = 100;
//野指针
int* q = (int*)0x1100;
cout << *q << endl;
return 0;
}

//const修饰指针
int constpoint() {
int a = 20;
//常量指针
const int* p = &a;
/*指针指向可以改
指针指向的值不可改*/
//指针常量
int* const q = &a;
//指针的指向不可以改
//指针指向的值可以改

const int* const x = &a;
//两者都不能改
return 0;
}
//指针和数组
int arrpoint() {
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr;
cout << *p << endl;
cout << *++p << endl;
int n = 0;
//cout << *++p << endl;
/*while (*p!=NULL)
{
n++;
p++;
}
cout <<"n的值为:"<< n << endl;*/
return 0;
}
//冒泡排序
int bubbleSort(int* a ,int length ) {

for (int i = 0; i < length-1;i++) {
for (int j = 0; j < length-i-1;j++) {
if (*(a+j)>*(a+j+1)) {
//cout<<"if中的"<< *(a + j + 1) <<endl;
int temp = *(a + j + 1);
*(a + j + 1) = *(a + j);
*(a + j) = temp;
}
}
}
for (int i =0 ; i < length;i++) {
cout << *(a + i)<<" ";
}
return 0;
}
//结构体

int structLearn() {
//创建学生类型

struct Student
{
string name;
int age;
int score;
} s3;
//通过学生类型创建具体的 变量
//在C++中结构体创建的时候,Struct可以省略
Student s1;
s1.name = "李四";
s1.age = 14;
s1.score = 90;
cout << "姓名" << s1.name << "年龄" << s1.age << "成绩" << s1.score << endl;
struct Student s2{"张三",13,91 };
cout << "姓名" << s2.name << "年龄" << s2.age << "成绩" << s2.score << endl;
s3.name = "王五";
s3.age = 12;
s3.score = 94;
cout << "姓名" << s3.name << "年龄" << s3.age << "成绩" << s3.score << endl;
return 0;

}

//结构体数组
int structArray(){
//创建结构体
struct Student
{
string name;
int age;
int score;
};
//创建结构体数组

struct Student stuArray[3] = {
{"张三",13,91},
{"李四",15,93},
{"王五",14,95}
};

stuArray[2].age = 18;
for (int i = 0; i < 3; i++) {
cout << "姓名" << stuArray[i].name << "年龄" << stuArray[i].age << "成绩" << stuArray[i].score << endl;
}
return 0;
}

//结构体指针
int structPoint() {
//第一学生的结构体
struct Student
{
string name;
int age;
int score;
};
//创建结构体变量
Student s = { "张三",13,91 };
//通过指针指向结构题变量
Student* p=&s;
//通过指针访问结构体变量中的数据
cout << "姓名:" <<p->name<<endl ;
cout << "年龄:" << p->age << endl;
cout << "成绩:" << p->score << endl;
return 0;
}
//结构体嵌套结构体
int strInStr() {
struct Student
{
string name;
int age;
int score;
}stu1 = {"张三",13,91};
struct Teacher
{
int id;
string name;
int age;
Student stu;
};
Teacher t1 = {1,"王五",30,stu1 };
Teacher* p1 = &t1;
cout << "姓名:" << p1->name << endl;
cout << "年龄:" << p1->age << endl;
cout << "编号:" << p1->id << endl;
cout << "所教学生:" << p1->stu.name << endl;
return 0;
}
//结构体做函数参数
struct Student
{
string name;
int age;
int score;
};
//值传递
void printStu(Student stu) {
cout << "姓名:" << stu.name << endl;
cout << "年龄:" << stu.age << endl;
cout << "成绩:" << stu.score << endl;
}
//地址传递
void printStu2(Student* p) {
cout << "姓名:" << p->name << endl;
cout << "年龄:" << p->age << endl;
cout << "成绩:" << p->score << endl;
p->age = 232;
}
int strArg() {
Student stu1 = { "张三",13,91 };
//printStu(stu1);
printStu2(&stu1);
return 0;
}
//const与结构体
void printStu2withConst(const Student* p) {
cout << "姓名:" << p->name << endl;
cout << "年龄:" << p->age << endl;
cout << "成绩:" << p->score << endl;
//p->age = 232;加入const可以防止误操作
}
int strConst() {
Student stu1 = { "张三",13,91 };
return 0;
}
int hero() {
struct Hero {
string name;
int age;
string sex;
};
Hero heros[5] = {
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"}
};
for (Hero hero:heros) {
cout << hero.name << hero.age << hero.sex << endl;
}
//冒泡开排
for (int i = 0; i < 5;i++) {
for (int j = 0; j < 4-i; j++) {
if (heros[j].age>heros[j+1].age)
{
Hero hero = heros[j];
heros[j] = heros[j + 1];
heros[j + 1] = hero;
}
}
}
cout << "-------------------"<< endl;
for (Hero hero : heros) {
cout << hero.name << hero.age << hero.sex << endl;
}
return 0;
}

//全局变量、静态变量、常量---都会放在全局区
int glob_a = 10;
int glob_b = 10;
const int glob_c = 10;
void vary() {
int a = 10;
int b = 10;
const int c = 10;
static int e = 10;
cout << "局部变量a的地址:" << (int)&a << endl;
cout << "局部变量b的地址:" << (int)&b << endl;


cout << "全局变量glob_a的地址:" << (int)&glob_a << endl;
cout << "全局变量glob_b的地址:" << (int)&glob_b << endl;
cout << "静态变量e的地址:" << (int)&e << endl;
cout << "字符串常量的地址:" << (int)&"hello" << endl;
//const修饰常量
cout << "const修饰的全局常量glob_c的地址:" << (int)&glob_c << endl;
cout << "const修饰的局部常量c的地址:" << (int)&c << endl;
}
//栈区
//栈区的数据由编译器管理开辟 和释放
//注意:不要返回局部变量的地址

int * func(int b) {//形参数据也会放在栈区
int a = 10; //栈区在函数执行后释放
return &a;
}
void usefunc() {
int* p = func(11);
cout << *p << endl;//第一次可以打印出来正确的结果,因为编译器会做一次保留
cout << *p << endl;
}
//堆区 由程序员来管理
int* func2() {
//利用new关键字,将数据开辟到堆区
//指针 本质也是局部变量,放在栈上,指针保存的数据是放在堆区
int* p = new int(10);
return p;
}
int usefunc2() {
int* p = func2();
cout << *p << endl;
return 0;
}
void test01() {
int* p = func2();
cout << *p << endl;
cout << *p << endl;
//若要删除堆区的数据需要用delete关键字
delete p;
cout << *p << endl;//内存已经释放,再访问就是非法操作
}
void test02() {
int * arr=new int[10];//创建一个数组
for (int i = 0; i < 10; i++) {
arr[i] = i + 100;
}
for (int i = 0; i < 10; i++) {
cout << arr[i] << endl;
}
//释放数组的时候要加一个中括号
delete[] arr;
}
//引用
void refrence1() {
int a = 10;
int& b = a;
cout << b << endl;
a = 120;
cout << b << endl;
b = 100;
cout << a << endl;
}
//引用必须初始化
//引用在初始化后, 不可以改变

//引用做函数参数
//作用 : 函故传参时,可以利用引用的技术让形参修饰实参
//优点:可以简化指针修改实参

void reference2() {
int a = 8, b = 10;
cout << a << b << endl;
swap(a,b);
cout << a << b << endl;
swap01(a, b);
cout << a << b << endl;
swap03(a, b);
cout << a << b << endl;
}
//引用做函数变量的返回值

//1.不要做局部变量的应用
int& test03() {
int a = 10;
return a;

}

//2.函数的调用可以做左值
int& test04() {
static int a = 10;//静态变量,存放在全局区,在程序结束后释放
return a;

}
void reference3() {
int& ref = test03();
cout << ref << endl;//编译器会保存一份数据
cout << ref << endl;//第二次的操作就是错误的

int& ref1 = test04();
cout << ref1 << endl;
cout << ref1 << endl;
test04() = 1000;
cout << ref1 << endl;
cout << ref1 << endl;
}
//引用的本质
void func0502(int & ref) {
ref = 100;
}
//引用的本质就是一个指针常量
int ref4() {
int a = 10;
int& ref = a;
ref = 20;
cout << a << endl;
cout << ref << endl;
return 0;
}

//常量引用
//使用场景:修饰形参,防止误操作
void ref5() {
int a = 10;
const int& ref = 10;//引用必须引用一块合法的内存空间
//加上const之后编译器将代码修改为 int temp=10;const int &ref=temp;
}
void showValue( const int& val) {
//val = 1000; 不允许修改
cout << val << endl;
}
void ref6() {
int a = 100;
showValue(a);
cout << "a:"<<a << endl;
}
//函数的默认参数
int func05021(int a,int b=100,int c=10) {
return a + b + c;
}
//注意:
//1.如果某个位置已经有了默认参数,则从这个位置往后,从左到右都必须有默认参数
//2.如果函数声明有默认参数,函数实现就不能有默认参数
void func05022() {
cout << func05021(10,20) << endl;
}

//占位参数
//只放一个数据类型用作占位
//目前阶段的占位参数用不到,占位参数可以有默认参数
//void func05031(int a, 10)
void func05031(int a,int) {
cout << "占位符" << endl;
}
void test05031() {
func05031(10,10);
}

//函数重载
//可以让函数名同名,提高函数的重用性
//·同一个作用域下
//·函数名称相同
//函数参数类型不同或者个数不同或者顺序不同
//函数的返回值不可以做为函数的重载的条件

void reload() {
cout << "函数reload()重载!" << endl;
}

void reload(int a) {
cout << "函数reload(int a)重载" << endl;
}
void reload(double a) {
cout << "函数reload(double a)重载" << endl;
}
void reload(double a,int b) {
cout << "函数reload(double a,int b)重载" << endl;
}

void reload(int a, double b) {
cout << "函数reload(int a, double b)重载" << endl;
}

void test05032() {
reload(10,1.1);
}

//函数重载的注意事项
//1、引用作为重载的条件
void reload1(int &a) {
cout << "reload1(int &a)调用" << endl;
}
void reload1(const int& a) {
cout << "reload1(const int& a)调用" << endl;
}
void test05033() {
int a = 10;
reload1(a);//会调用不带const的
//reload1(10);//会调用带const的
}
//2、函数重载碰到默认参数
void reload2(int a,int b=10) {
cout<<"reload2(int a)"<<endl;
}
void reload2(int a) {
cout << "reload2(int a)" << endl;
}
//可以编译通过 调用时会出错,二义性,调用函数的时候调用两个参数不会报错

int main() {
cout << "MAIN" << endl;
//helloworld();
/*int a = 0, b = 1;
swap(a,b);
cout << a << b << endl;*/
/*int a[] = { 1,2,3,4,5,6,7 };
arrayResive(a);*/
//cout << add(80, 50) << endl;
/*point();*/
/*int arr[] = {10,9,8,7,6,5,4,3,2,1};
int a=sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr,a);*/
/*arrpoint();*/
//structLearn();
//structArray();
/*structPoint();*/
//strInStr();
//strArg();
//hero();
//vary();
//test01();
//reference3();
//ref6();
//func05022();
//test05032();
test05033();
system("pause");
}