修改密码

【2024课程】零基础学会C++编程课程

成品课程

陈远龙老师主讲 & 答疑

课程题单 - T1000

未购买 · 可先试学5节课

课程目录展开/折叠

第73课 STL中常用算法函数和类

播放快捷键

播放/暂停:空格(或鼠标单击)      全屏:F(或鼠标双击)      退出全屏:Esc

快进10 / 30 / 60秒:方向键→ / Ctrl + 方向键→ / Shift + 方向键→

快退10 / 30 / 60秒:方向键← / Ctrl + 方向键← / Shift + 方向键←

本节课讲解配套PPT&板书:

本节课讲解到的源代码

源代码下载:第73课 STL中常用算法函数和类-源代码下载

1. STL中常见的算法函数-1
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // min
    int a = 1, b = 2;
    cout << min(a, b) << endl;

    double d1 = 3.4, d2 = -1.2;
    cout << min(d1, d2) << endl;

    // max
    cout << max(a, b) << endl;
    cout << max(d1, d2) << endl;

    // swap
    cout << a << ' ' << b << endl;
    swap(a, b); // swap(int&, int&)
    cout << a << ' ' << b << endl;

    return 0;
} 
2. STL中常见的算法函数-2-sort
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // sort
    int a[5] = {1, 3, 2, 4, 5};
    // sort(a, a + 5); // [l, r) => [l, r - 1] 左闭右开 
    // sort(a, a + 5, greater<int>());
    sort(a, a + 5, less<int>());
    for (int t : a)
        cout << t << ' ';

    return 0;
} 
3. STL中常见的算法函数-3-sort
#include <bits/stdc++.h>
using namespace std;

struct Student {
    int id;
    char name[20];
    double h;
};

Student s[10];

// 独立的<重载函数 自定义排序规则 
bool operator<(Student& s1, Student& s2)
{
    /*
    if (s1.id < s2.id) return true;
    else return false;
    */
    return s1.id < s2.id;
}

int main()
{
    srand(time(NULL));
    for (int i = 0; i < 10; i ++)
    {
        s[i].id = rand() % 100; // 0 - 99
        cout << s[i].id << ' ';
        // s[i].name s[i].h
    }
    cout << endl;

    // cout << (s[1] < s[2]) << endl;
    // 默认使用<进行排序 
    sort(s, s + 10); // s[1] s[2] s[1] < s[2]

    for (auto t : s)
        cout << t.id << ' ';
    cout << endl;

    return 0;
} 
4. STL中常见的算法函数-4-sort
#include <bits/stdc++.h>
using namespace std;

struct Student {
    int id;
    char name[20];
    double h;
    // 成员函数的方式重载< 
    bool operator<(Student& b)
    {
        return id < b.id;
        // return id > b.id;
    }
};

Student s[10];

int main()
{
    srand(time(NULL));
    for (int i = 0; i < 10; i ++)
    {
        s[i].id = rand() % 100; // 0 - 99
        cout << s[i].id << ' ';
        // s[i].name s[i].h
    }
    cout << endl;

    // cout << (s[1] < s[2]) << endl;
    // 默认使用<进行排序 
    sort(s, s + 10); // s[1] s[2] s[1] < s[2]

    for (auto t : s)
        cout << t.id << ' ';
    cout << endl;

    return 0;
} 
5. STL中常见的算法函数-5-sort
#include <bits/stdc++.h>
using namespace std;

struct Student {
    int id;
    char name[20];
    double h;
};

Student s[10];

// 针对于比较函数 推荐使用 
bool cmp(Student& s1, Student& s2)
{
    // return s1.id < s2.id;
    return s1.id > s2.id;
}

int main()
{
    srand(time(NULL));
    for (int i = 0; i < 10; i ++)
    {
        s[i].id = rand() % 100; // 0 - 99
        cout << s[i].id << ' ';
        // s[i].name s[i].h
    }
    cout << endl;

    sort(s, s + 10, cmp); // s[1] s[2] s[1] < s[2]

    for (auto t : s)
        cout << t.id << ' ';
    cout << endl;

    return 0;
} 
6. string-1
#include <bits/stdc++.h>
using namespace std;

/*
class string {
    private:
        // 成员字段
        char *p;
        int n;
    public:
        // 成员函数 
        string() 默认的构造函数 
        {
            //...
        }
        string(int n = 10)
        {
            // ...
        }
};
*/

/*
istream& operator>>(istream& is, string& s)
{
}

ostream& operator>>(ostream& os, string& s)
{
}
*/

int main()
{
    // s1调用了默认构造函数 默认构造的是一空字符串 ("" or size == 0) 
    // string s1(); // 不能这样调用 
    string s1; // s1是string类的变量、string类的实例对象
    cout << s1 << endl;
    cout << s1.size() << endl; // s1.length(); 不是通过strlen函数 
    cout << s1.length() << endl; // 具有语义化,长度 
    cin >> s1;  // >> 重载 
    cout << s1 << endl; // << 重载 

    /*
    char s2[10];
    cin >> s2;
    cout << s2 << endl;
    */

    return 0;
} 
7. string-2
#include <bits/stdc++.h>
using namespace std;

/*
class string {
    private:
        // 成员字段
        char *p;
        int n;
    public:
        // 成员函数 
        string() 默认的构造函数 
        {
            //...
        }
        string(int n = 10)
        {
            // ...
        }
        string(const char *s)
        {
            p = new char[strlen(s) + 1];
            strcpy(p, s); // strncpy
            n = strlen(s);
        }
        string(const string &s)
        {
        }
};
*/

/*
istream& operator>>(istream& is, string& s)
{
}

ostream& operator>>(ostream& os, string& s)
{
}
*/

int main()
{
    string s1("xiaoming"); // const char *
    cout << s1.size() << endl;

    string s2(s1); // 调用拷贝构造函数
    cout << s2 << endl;

    s2[0] = 'y'; // 重载了[]运算符 
    cout << s2 << endl; // yiaoming
    cout << s1 << endl;

    s2 = "lier"; // 重载了=运算符 函数 
    cout << s2 << endl;

    return 0;
} 
8. string-3
#include <bits/stdc++.h>
using namespace std;

ostream& operator<<(ostream& os, string& s)
{
    // cout << "my <<" <<endl;
    // os << s << endl;
    os << "自己实现的<<函数" << endl;
    for (int i = 0; i < s.size(); i ++)
    {
        os << s[i];
    }
    return os;
}

int main()
{
    string s2 = "lier"; 
    cout << s2 << endl;

    return 0;
} 
9. string-4
#include <bits/stdc++.h>
using namespace std;

/*
string operator+(string& s1, const char *p)
{
    // ...
}
*/

int main()
{
    /*
    string s1 = "xiaoming"; 
    string s2 = s1 + "hello"; // + 重载 string operator+(string& s1, const char *p)
    cout << s2 << endl;
    s2.append(",world");
    cout << s2 << endl;

    s2 = "ni hao" + s2; // string operator+(const char *p, string& s1)

    cout << s2 << endl;

    // char + string string + char
    */
    string s3 = "xiaoming";
    s3 = s3 + 'a'; // string operator+(string& s1, char ch);
    cout << s3 << endl;

    s3 = 'c' + s3; // string operator+(char ch, string &s1);
    cout << s3 << endl;

    string s4 = " cpp";
    cout << s3 + s4; // string operator+(string &s1, string &s2);

    return 0;
} 
10. string-5
#include <bits/stdc++.h>
using namespace std;

/*
typedef unsigned int size_t;
typedef unsigned long long size_t;
*/

int main()
{
    string s1("xiaoming");
    // size_t pos = s1.find("mi");
    size_t pos = s1.find("mia"); // pos string::npos
    cout << pos << endl;
    cout << string::npos << endl; // ULLONG_MAX
    cout << ULLONG_MAX << endl;
    cout << UINT_MAX << endl;
    cout << SIZE_MAX << endl;
    cout << sizeof(size_t) << endl;

    return 0;
} 
11. vector-1
#include <bits/stdc++.h>
using namespace std;

int main()
{
    /* 
    int a[5] = {1, 2, 3, 4, 5}; // 固定大小的数组 
    a[5] = 10; // 数组访问越界 

    int n = 10;
    int b[n]; // VLA C99,不推荐使用 
    */

    vector<int> v1;
    cout << v1.size() << endl;
    // v1.push_back(1);
    for (int i = 0; i < 10; i ++)
        v1.push_back(i);
    cout << v1.size() << endl;

    return 0;
} 
12. vector-2
#include <bits/stdc++.h>
using namespace std;

int main()
{
    /*
    vector<int> v1;
    for (int i = 0; i < 10; i ++)
        v1.push_back(i);
    for (int i = 0; i < 10; i ++)
        cout << v1[i] << ' ';
    cout << endl;
    v1[3] = 30;
    for (int t : v1)
        cout << t << ' ';
    */

    // vector<int> v2;
    vector<int> v2(10);
    /*
    for (int i = 0; i < 10; i ++)
        v2[i] = i; // 必须是存在的元素才能[]访问 
    */
    for (int t : v2)
        cout << t << ' ';

    return 0;
} 
13. vector-3
#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> v1(100, 5);
    cout << v1.size() << endl;
    for (int t : v1)
        cout << t << ' ';
    return 0;
} 
14. vector-4
#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> v1 = {1, 2, 3};
    v1.reserve(200); // 改变的是容量最大大小 
    // v1.resize(13); // 改编的是实际的元素的个数 
    v1.resize(13, 5); 
    cout << v1.size() << endl; // 实际的元素个数 
    cout << v1.capacity() << endl; // 最大的容量 

    for (int t: v1)
        cout << t << ' ';
    cout << endl;

    for (int i = 0; i < 100; i ++)
    {
        v1.push_back(i);
        cout << "size:" << v1.size() << endl;
        cout << "capacity:" << v1.capacity() << endl;
    }

    return 0;
} 
15. vector-5
#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> v1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    for (int i = 0; i < v1.size(); i ++)
        cout << v1[i] << ' ';
    cout << endl;

    for (int t : v1)
        cout << t << ' ';
    cout << endl;

    for (vector<int>::iterator it = v1.begin(); it != v1.end(); it ++)
    {
        cout << *it << ' ';
    }
    cout << endl;

    for (auto it = v1.begin(); it != v1.end(); it ++)
    {
        cout << *it << ' ';
    }
    cout << endl;

    sort(v1.begin(), v1.end(), greater<int>());    

    for (auto it = v1.begin(); it != v1.end(); it ++)
    {
        cout << *it << ' ';
    }
    cout << endl;  

    reverse(v1.begin(), v1.end()); 

    for (int t : v1)
        cout << t << ' ';
    cout << endl;   

    return 0;
} 
16. vector-6-实现动态二维数组
#include <bits/stdc++.h>
using namespace std;

int main()
{
    /* 
    int a[3][4]; 

    vector<vector<int>> v1 = {
        {1, 2, 3},
        {4, 5},
        {9}
    };
    for (int i = 0; i < v1.size(); i ++)
    {
        // v[i]
        for (int j = 0; j < v1[i].size(); j ++)
            cout << v1[i][j] << ' ';
        cout << endl;
    }
    */

    /*
    vector<vector<int>> v2;

    for (int i = 0; i < 5; i ++)
    {
        v2.push_back(vector<int>(4, 10));;
    }
    for (vector<int> t : v2)
    {
        for (int k : t)
            cout << k << ' ';
        cout << endl;
    }
    */

    vector<vector<int>> v3(3, vector<int>(4, 0));
    for (int i = 0; i < v3.size(); i ++)
    {
        for (int j = 0; j < v3[i].size(); j ++)
            cout << v3[i][j] << ' ';
        cout << endl;
    }

    return 0;
} 
17. stack-1
#include <bits/stdc++.h>
using namespace std;

int main()
{
    stack<int> s;
    for (int i = 0; i < 10; i ++)
        s.push(i);

    while (!s.empty())
    {
        cout << s.top() << ' ';
        s.pop();
    }

    return 0;

} 
18. queue-1
#include <bits/stdc++.h>
using namespace std;

int main()
{
    queue<int> q;
    for (int i = 0; i < 10; i ++)
        q.push(i);

    while (!q.empty())
    {
        cout << q.front() << ' ';
        q.pop();
    }

    return 0;

} 
本节课无课后练习

本节课答疑

建议大家有问题先通过AI答疑(比如:DeepSeek 等),AI时代需要学会使用AI辅助学习

陈远龙老师视频讲解:如何使用DeepSeek进行答疑?

通过AI未能获得满意解答的,可以联系陈远龙老师答疑

目录