202111051401结构体.mp4

大纲要求

•【3】结构体的定义及应用


User-defined structs

在实际问题中,一组数据往往有不同的数据类型,比如学生,有姓名、学号、语文成绩、数学成绩等等。C++语言给出了一种构造数据类型—“结构体”,结构体在数据存储方面相当于其他高级语言中的记录,但它悠着面向对象的优势。

  1. struct stu{
  2. string name;
  3. int id;
  4. int chinese, math;
  5. };

结构体的构造函数

https://blog.csdn.net/a_forever_dream/article/details/88867801

  1. // struct构造函数,说白了,就是初始化
  2. // 下面这段代码,看起来很奇怪,可是一定要很熟悉的啊
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. struct node{
  6. int x, y;
  7. node(int _x = 0, int _y = 0): x(_x), y(_y){}
  8. };
  9. int main(){
  10. node(2, 3);
  11. return 0;
  12. }

结构体的小于符号重载

image.png
image.png

  1. // 下面这段代码,看起来很奇怪,可是一定要很熟悉的啊
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. struct node{
  5. int x, y;
  6. node(int _x = 0, int _y = 0): x(_x), y(_y){}
  7. bool operator< (const node& W)const{
  8. if (x == W.x) return y < W.y;
  9. return x < W.x;
  10. }
  11. };
  12. int main(){
  13. node A = node(2, 3);
  14. node B = node(3, 4);
  15. if (A < B) cout << '<' << '\n';
  16. else cout << ">=" << '\n';
  17. return 0;
  18. }

sort()函数

sort函数的特点是非常好用、易用,比赛中,利用这个特点设置坑,让你跳。比如,不管时间复杂度,或者不会看时间复杂度,上来就是sort,然后就肯定超时。就是用sort,也会导致,对数组和下标的理解不完备,导致没有手搓代码的能力。

  1. // 先看一下sort的自定义
  2. // 自定义一个cmp函数
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. int a[110];
  6. bool cmp(int a, int b){
  7. return a > b;
  8. }
  9. int main()
  10. {
  11. int n;
  12. cin >> n;
  13. for (int i = 0; i < n; i++) cin >> a[i];
  14. sort(a, a + n, cmp);
  15. for (int i = 0; i < n; i++) cout << a[i] << ' ';
  16. puts("");
  17. return 0;
  18. }
  1. // 如果只是从大到小,还可以使用greater<int>()
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int a[110];
  5. int main()
  6. {
  7. int n;
  8. cin >> n;
  9. for (int i = 0; i < n; i++) cin >> a[i];
  10. sort(a, a + n, greater<int>());
  11. for (int i = 0; i < n; i++) cout << a[i] << ' ';
  12. puts("");
  13. return 0;
  14. }
  1. // 除了自定义函数,可以结构体的小于符号重载
  2. // 以下两个代码,实战意义很大,务必熟练操作
  3. // 下面这段代码,看起来很奇怪,可是一定要很熟悉的啊
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. const int N = 110;
  7. struct node{
  8. int x, y;
  9. node(int _x = 0, int _y = 0): x(_x), y(_y){}
  10. bool operator< (const node& W)const{
  11. if (x == W.x) return y < W.y;
  12. return x < W.x;
  13. }
  14. }a[N];
  15. int n;
  16. int main(){
  17. cin >> n;
  18. for (int i = 0; i < n; i++) cin >> a[i].x >> a[i].y;
  19. sort(a, a + n);
  20. for (int i = 0; i < n; i++) cout << a[i].x << ' ' << a[i].y << '\n';
  21. return 0;
  22. }
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 110;
  4. struct node{
  5. int x, y;
  6. node(int _x = 0, int _y = 0): x(_x), y(_y){}
  7. }a[N];
  8. int n;
  9. bool cmp(node a, node b){
  10. if (a.x == b.x) return a.y < b.y;
  11. return a.x < b.x;
  12. }
  13. int main(){
  14. cin >> n;
  15. for (int i = 0; i < n; i++) cin >> a[i].x >> a[i].y;
  16. sort(a, a + n, cmp);
  17. for (int i = 0; i < n; i++) cout << a[i].x << ' ' << a[i].y << '\n';
  18. return 0;
  19. }

image.png


例题 病人排队

例题【07NOIP普及组】奖学金

例题【09NOIP普及组】分数线划定