第十三课 标准模板库(STL)

标准模板库STL是C++的核心优势之一,它提供了丰富的容器和算法,让我们可以高效地完成常见编程任务。

一、vector(动态数组)

#include <iostream>
#include <vector>
using namespace std;

int main() {
    // 创建vector
    vector<int> v;           // 空vector
    vector<int> v2(5);       // 5个元素,默认0
    vector<int> v3(5, 100); // 5个元素,都是100
    vector<int> v4 = {1, 2, 3, 4, 5};  // 初始化列表

    // 添加元素
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);

    // 访问元素
    cout << v[0] << endl;    // 1
    cout << v.at(1) << endl; // 2(带边界检查)

    // 大小
    cout << v.size() << endl;   // 3

    // 遍历
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
    cout << endl;

    // 范围for遍历(C++11)
    for (int x : v) {
        cout << x << " ";
    }
    cout << endl;

    // 删除元素
    v.pop_back();  // 删除最后一个
    v.clear();     // 清空所有

    // 常用方法
    v.empty();   // 是否为空
    v.front();   // 第一个元素
    v.back();    // 最后一个元素

    return 0;
}

vector常用操作

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

int main() {
    vector<int> v = {5, 2, 8, 1, 9, 3};

    // 排序
    sort(v.begin(), v.end());  // 1 2 3 5 8 9

    // 逆序
    reverse(v.begin(), v.end());  // 9 8 5 3 2 1

    // 查找
    auto it = find(v.begin(), v.end(), 5);
    if (it != v.end()) {
        cout << "找到5" << endl;
    }

    // 最大最小
    cout << *max_element(v.begin(), v.end()) << endl;  // 9
    cout << *min_element(v.begin(), v.end()) << endl;  // 1

    // 累加
    int sum = accumulate(v.begin(), v.end(), 0);
    cout << sum << endl;  // 1+2+3+5+8+9=28

    // 交换
    vector<int> v2 = {10, 20};
    swap(v[0], v[1]);

    return 0;
}

vector二维数组

#include <iostream>
#include <vector>
using namespace std;

int main() {
    // 二维vector
    vector<vector<int>> matrix(3, vector<int>(4, 0));

    // 3行4列的矩阵
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            matrix[i][j] = i * 4 + j + 1;
        }
    }

    // 输出
    for (int i = 0; i < matrix.size(); i++) {
        for (int j = 0; j < matrix[i].size(); j++) {
            cout << matrix[i][j] << "\t";
        }
        cout << endl;
    }

    return 0;
}

二、string(字符串)

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "Hello";

    // 长度
    cout << s.length() << endl;  // 5
    cout << s.size() << endl;     // 5

    // 访问
    cout << s[0] << endl;   // H
    cout << s.at(1) << endl; // e

    // 拼接
    s += " World";
    cout << s << endl;  // Hello World

    // 查找
    size_t pos = s.find("World");
    cout << pos << endl;  // 6

    // 子串
    cout << s.substr(0, 5) << endl;  // Hello

    // 替换
    s.replace(6, 5, "C++");
    cout << s << endl;  // Hello C++

    return 0;
}

三、deque(双端队列)

#include <iostream>
#include <deque>
using namespace std;

int main() {
    deque<int> d;

    // 两端操作
    d.push_back(1);   // 后端添加
    d.push_back(2);
    d.push_front(0);  // 前端添加

    for (int x : d) {
        cout << x << " ";
    }
    cout << endl;  // 0 1 2

    d.pop_back();    // 删除最后一个
    d.pop_front();   // 删除第一个

    cout << d.front() << endl;  // 1
    cout << d.back() << endl;   // 1

    return 0;
}

四、list(双向链表)

#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> l = {1, 2, 3, 4, 5};

    // 链表特点:插入删除高效,随机访问低效
    l.push_back(6);   // 末尾添加
    l.push_front(0);  // 开头添加

    // 遍历(双向迭代器)
    for (int x : l) {
        cout << x << " ";
    }
    cout << endl;  // 0 1 2 3 4 5 6

    // 在某个位置插入
    auto it = l.begin();
    ++it;  // 指向第二个元素
    l.insert(it, 100);  // 在位置1插入100

    for (int x : l) {
        cout << x << " ";
    }
    cout << endl;  // 0 100 1 2 3 4 5 6

    return 0;
}

五、set(集合)

#include <iostream>
#include <set>
using namespace std;

int main() {
    set<int> s = {3, 1, 4, 1, 5, 9, 2, 6};

    // 自动排序(从小到大),去重
    for (int x : s) {
        cout << x << " ";
    }
    cout << endl;  // 1 2 3 4 5 6 9

    // 插入
    s.insert(10);
    s.insert(5);  // 5已存在,不会重复插入

    // 查找
    if (s.find(5) != s.end()) {
        cout << "找到5" << endl;
    }

    // 删除
    s.erase(3);

    // 常用方法
    s.size();   // 元素个数
    s.empty();  // 是否为空
    s.count(x); // 元素x出现的次数(0或1)

    return 0;
}

六、map(映射)

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    map<string, int> m;

    // 插入方式
    m["apple"] = 5;
    m["banana"] = 3;
    m.insert(make_pair("orange", 8));
    m.insert({"grape", 6});

    // 访问
    cout << m["apple"] << endl;  // 5

    // 查找
    if (m.find("banana") != m.end()) {
        cout << "banana的值:" << m["banana"] << endl;
    }

    // 遍历
    for (auto &p : m) {
        cout << p.first << " : " << p.second << endl;
    }

    // 常用方法
    m.size();    // 元素个数
    m.erase("apple");  // 删除
    m.empty();  // 是否为空

    return 0;
}

七、stack(栈)

#include <iostream>
#include <stack>
using namespace std;

int main() {
    stack<int> s;

    s.push(1);   // 入栈
    s.push(2);
    s.push(3);

    cout << s.top() << endl;   // 3(查看栈顶)
    s.pop();                  // 出栈(删除栈顶)

    cout << s.top() << endl;  // 2

    cout << s.empty() << endl;  // 0(不为空)
    cout << s.size() << endl;   // 2

    return 0;
}

八、queue(队列)

#include <iostream>
#include <queue>
using namespace std;

int main() {
    queue<int> q;

    q.push(1);   // 入队
    q.push(2);
    q.push(3);

    cout << q.front() << endl;  // 1(查看队首)
    q.pop();                    // 出队(删除队首)

    cout << q.front() << endl;  // 2
    cout << q.back() << endl;   // 3(查看队尾)

    return 0;
}

九、priority_queue(优先队列)

#include <iostream>
#include <queue>
using namespace std;

int main() {
    priority_queue<int> pq;  // 默认大根堆

    pq.push(3);
    pq.push(1);
    pq.push(5);
    pq.push(2);

    // 每次取最大的
    while (!pq.empty()) {
        cout << pq.top() << " ";
        pq.pop();
    }
    cout << endl;  // 5 3 2 1

    // 小根堆
    priority_queue<int, vector<int>, greater<int>> pq2;
    pq2.push(3);
    pq2.push(1);
    pq2.push(5);

    while (!pq2.empty()) {
        cout << pq2.top() << " ";
        pq2.pop();
    }
    cout << endl;  // 1 3 5

    return 0;
}

十、迭代器使用

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3, 4, 5};

    // 迭代器遍历
    for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    // C++11简化
    for (auto it = v.begin(); it != v.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    // 范围for
    for (int x : v) {
        cout << x << " ";
    }
    cout << endl;

    return 0;
}

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注