<返回目录     Powered by claude/xia兄

第14课: STL标准库

vector容器

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

int main() {
    // 创建vector
    vector<int> vec;

    // 添加元素
    vec.push_back(10);
    vec.push_back(20);
    vec.push_back(30);

    // 访问元素
    cout << "第一个元素:" << vec[0] << endl;
    cout << "最后一个元素:" << vec.back() << endl;

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

    // 范围for循环
    for (int num : vec) {
        cout << num << " ";
    }
    cout << endl;

    // 插入和删除
    vec.insert(vec.begin() + 1, 15);  // 在位置1插入15
    vec.erase(vec.begin());           // 删除第一个元素
    vec.pop_back();                   // 删除最后一个元素

    // 清空
    vec.clear();
    cout << "清空后大小:" << vec.size() << endl;

    return 0;
}

map容器

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

int main() {
    // 创建map
    map<string, int> scores;

    // 插入元素
    scores["张三"] = 85;
    scores["李四"] = 90;
    scores["王五"] = 78;
    scores.insert(make_pair("赵六", 92));

    // 访问元素
    cout << "张三的分数:" << scores["张三"] << endl;

    // 查找元素
    if (scores.find("李四") != scores.end()) {
        cout << "找到李四" << endl;
    }

    // 遍历
    for (auto pair : scores) {
        cout << pair.first << ": " << pair.second << endl;
    }

    // 删除元素
    scores.erase("王五");

    // 大小
    cout << "map大小:" << scores.size() << endl;

    return 0;
}

set容器

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

int main() {
    // 创建set(自动排序,不允许重复)
    set<int> s;

    // 插入元素
    s.insert(30);
    s.insert(10);
    s.insert(20);
    s.insert(10);  // 重复元素不会被插入

    // 遍历(自动排序)
    for (int num : s) {
        cout << num << " ";
    }
    cout << endl;

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

    // 删除
    s.erase(10);

    // 大小
    cout << "set大小:" << s.size() << endl;

    return 0;
}

list容器

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

int main() {
    // 创建list(双向链表)
    list<int> lst;

    // 添加元素
    lst.push_back(10);
    lst.push_back(20);
    lst.push_front(5);   // 在前面添加

    // 遍历
    for (int num : lst) {
        cout << num << " ";
    }
    cout << endl;

    // 排序
    lst.sort();

    // 反转
    lst.reverse();

    // 删除
    lst.pop_front();
    lst.pop_back();

    return 0;
}

stack和queue

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

int main() {
    // 栈(后进先出)
    stack<int> stk;
    stk.push(10);
    stk.push(20);
    stk.push(30);

    cout << "栈顶元素:" << stk.top() << endl;
    stk.pop();
    cout << "弹出后栈顶:" << stk.top() << endl;

    // 队列(先进先出)
    queue<int> q;
    q.push(10);
    q.push(20);
    q.push(30);

    cout << "队首元素:" << q.front() << endl;
    cout << "队尾元素:" << q.back() << endl;
    q.pop();
    cout << "出队后队首:" << q.front() << endl;

    return 0;
}

STL算法

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

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

    // 排序
    sort(vec.begin(), vec.end());
    cout << "排序后:";
    for (int num : vec) cout << num << " ";
    cout << endl;

    // 反转
    reverse(vec.begin(), vec.end());
    cout << "反转后:";
    for (int num : vec) cout << num << " ";
    cout << endl;

    // 查找
    auto it = find(vec.begin(), vec.end(), 8);
    if (it != vec.end()) {
        cout << "找到8,位置:" << (it - vec.begin()) << endl;
    }

    // 最大值和最小值
    cout << "最大值:" << *max_element(vec.begin(), vec.end()) << endl;
    cout << "最小值:" << *min_element(vec.begin(), vec.end()) << endl;

    // 计数
    int count = count_if(vec.begin(), vec.end(), [](int x) { return x > 5; });
    cout << "大于5的元素个数:" << count << endl;

    return 0;
}

迭代器

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

int main() {
    vector<int> vec = {10, 20, 30, 40, 50};

    // 正向迭代器
    cout << "正向遍历:";
    for (auto it = vec.begin(); it != vec.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    // 反向迭代器
    cout << "反向遍历:";
    for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    // 常量迭代器
    for (auto it = vec.cbegin(); it != vec.cend(); ++it) {
        cout << *it << " ";
        // *it = 100;  // 错误!不能修改
    }
    cout << endl;

    return 0;
}

练习题

  1. 使用vector实现学生成绩管理系统
  2. 使用map统计字符串中每个单词出现的次数
  3. 使用set实现集合的交集、并集、差集运算
  4. 使用stack实现表达式求值(后缀表达式)
  5. 使用STL算法对自定义对象进行排序