ZX3 Python 和 STL 的使用

STL

  一、常用的函数 <algorithm>

    sort

    1. 左闭右开区间,从小到大排序
    2. 改写为从大到小排序
      1 int cmp(int a, int b)
      2 {
      3     return a>b;
      4 }
      5 
      6 sort(a, a+n, cmp);
    3.  给结构体排序
       1 #include <bits/stdc++.h>
       2 using namespace std;
       3 
       4 // 以分数为第一关键字,学号为第二关键字给学生排序
       5 
       6 /* 写法一,写cmp函数
       7 
       8 struct student
       9 {
      10     int score, id;
      11 }a[10];
      12 
      13 bool cmp(student a, student b)
      14 {
      15     if(a.score!=b.score) return a.score<b.score;
      16     return a.id<b.id;
      17 }*/
      18 
      19 // 写法二,在结构体中定义小于号(重载小于号)
      20 
      21 struct student
      22 {
      23     int score, id;
      24 
      25     // 写在里面要加一个 friend,先背下来
      26     friend bool operator < (student a, student b)
      27     {
      28         if(a.score!=b.score) return a.score<b.score;
      29         return a.id<b.id;
      30     }
      31 
      32 }a[10];
      33 /* 写在外面的写法
      34 bool operator < (student a, student b)
      35 {
      36     if(a.score!=b.score) return a.score<b.score;
      37     return a.id<b.id;
      38 }*/
      39 
      40 int main()
      41 {
      42     a[0].score=100, a[0].id=1;
      43     a[1].score=100, a[1].id=2;
      44     a[2].score=90, a[2].id=3;
      45     a[3].score=50, a[3].id=4;
      46     a[4].score=80, a[4].id=5;
      47 
      48     //sort(a, a+5, cmp);
      49     sort(a, a+5);
      50 
      51     for(int i=0; i<5; i++)
      52         printf("%d %d\n", a[i].score, a[i].id);
      53 
      54     return 0;
      55 }
      struct_sort

    pair

    1.  将两个数打包成一个
       1 #include <bits/stdc++.h>
       2 using namespace std;
       3 
       4 //普通的函数返回值只能有一个,但用 pair 可以一次性返回两个值
       5 pair<int, int> pr;
       6 
       7 int main()
       8 {
       9     // make_pair 是一个函数,会自动判断括号里的类型,打包成一个对应的 pair
      10     pr = make_pair(1, 2)  
      11     
      12     // pair 默认先按 first 比较,再按 second
      13     cout<<pr.first<<endl;<<pr.second<<endl;
      14 
      15     return 0;
      16 }
      pair

  二、STL 中的容器

    vector <vector>

    1.  可以动态分配空间的(数组。。类似)
       1 #include <bits/stdc++.h>
       2 using namespace std;
       3 
       4 vector<int> v;
       5 
       6 int main()
       7 {
       8     // 尾部压进元素
       9     v.push_back(1);
      10     v.push_back(3);
      11     v.push_back(2);
      12     v.push_back(5);
      13     v.push_back(4);
      14     
      15     // 尾部弹出元素
      16     v.pop_back();
      17     
      18     // 支持下标访问(随机访问)
      19     for(int i=0; i<v.size(); i++)
      20         cout<<v[i]<<endl;  
      21     
      22     // 排序
      23     sort(v.begin(), v.end());  // v.begin 和 v.end 类型是迭代器
      24     
      25     return 0;
      26 }
      vector
    2.  对结构体进行操作
       1 #include <bits/stdc++.h>
       2 using namespace std;
       3 
       4 struct student
       5 {
       6     int score, id;
       7 
       8     // 构造函数:初始化一个 student 结构体的时候,会调用构造函数
       9     // 即和 struct 同名的函数
      10     student(int _id=0, int _score=0)
      11     {
      12         id=_id;
      13         score=_score;
      14     }
      15 
      16 };
      17 
      18 vector<student> v;
      19 
      20 int main()
      21 {
      22     v.push_back(student(1, 2));
      23     v.push_back(student(3, 4));
      24     v.push_back(student(3, 4));
      25     v.push_back(student(5, 6));
      26     v.push_back(student(6, 8));
      27 
      28     for(int i=0; i<v.size(); i++)
      29         cout<<v[i].score<<endl;
      30 
      31     return 0;
      32 }
      struct_vector

     <stack>

    1.  1 #include <bits/stdc++.h>
       2 using namespace std;
       3 
       4 int main()
       5 {
       6     stack<int> s;
       7 
       8     s.push(1);
       9     s.push(2);
      10     s.push(3);
      11     s.push(4);
      12     
      13     cout<<s.size()<<endl;
      14 
      15     while(!s.empty()){
      16         cout<<s.top();
      17         s.pop();
      18     }
      19 
      20     return 0;
      21 }
      Stack

       队列 <queue>

    1.   
       1 #include <bits/stdc++.h>
       2 using namespace std;
       3 
       4 int main()
       5 {
       6     queue<int> s;
       7 
       8     s.push(1);
       9     s.push(2);
      10     s.push(3);
      11     s.push(4);
      12 
      13     cout<<s.size()<<endl;
      14 
      15     while(!s.empty()){
      16         cout<<s.front();
      17         s.pop();
      18     }
      19 
      20     return 0;
      21 }
      Queue

猜你喜欢

转载自www.cnblogs.com/mianing/p/12574969.html