蓝桥杯算法练习笔记(5)__常用的STL
发布日期:2021-04-30 21:04:18 浏览次数:74 分类:精选文章

本文共 5234 字,大约阅读时间需要 17 分钟。

常用STL容器深入解析

1. 动态数组 - Vector

Vector是C++中最常用的动态数组容器,支持任意类型的元素存储与操作。

1.1 基本操作

  • 元素存储:可以通过push_back方法向容器尾部添加元素。
  • 元素修改:使用[]运算符修改指定位置的元素。
  • 元素删除:使用pop_back方法删除尾部元素,或者使用erase方法删除任意位置的元素。
  • 遍历:通过迭代器从头到尾遍历所有元素。
#include 
#include
using namespace std;vector
vec;vec.push_back(1);vec.push_back(2);vec.push_back(3); // 向量变为 [1, 2, 3]vec[1] = 4; // 修改第二个元素为4,向量变为 [4, 2, 3]vec.pop_back(); // 删除第三个元素,向量变为 [4, 2]for (int i = 0; i < vec.size(); ++i) { cout << vec[i] << " ";}

1.2 存储自定义数据类型

Vector可以存储自定义数据类型的对象,例如结构体。

#include 
#include
#include
#include
struct Student { string name; int age;};vector
class1;Student stu1, stu2;stu1.name = "Adime";stu2.name = "Sada";stu1.age = 19;stu2.age = 28;class1.push_back(stu1);class1.push_back(stu2);for (const auto& stu : class1) { cout << stu.name << " " << stu.age << endl;}

2. 二维动态数组

使用Vector容器来创建和操作二维动态数组,可以通过resize方法动态调整行列数。

2.1 初始化

vector
> vec2(n, vector
(m, 0));

2.2 赋值与遍历

for (int i = 0; i < n; ++i) {    for (int j = 0; j < m; ++j) {        cin >> vec2[i][j];    }}for (int i = 0; i < vec2.size(); ++i) {    for (int j = 0; j < vec2[i].size(); ++j) {        cout << vec2[i][j] << " ";    }    cout << endl;}

3. 集合 - Set

Set是一个自动排序的单元素集合,元素只能唯一存在。

3.1 基本操作

  • 插入:使用insert方法插入元素,重复插入不会报错。
  • 删除:使用erase方法删除元素,不存在的元素不会报错。
  • 查找:使用count方法检查元素是否存在。
  • 遍历:使用迭代器逐个访问集合中的元素。
#include 
#include
using namespace std;set
se;se.insert(1);se.insert(2);se.insert(3);se.insert(0);se.erase(1);se.erase(5);if (se.count(2)) { cout << "2存在." << endl;}for (set
::iterator it = se.begin(); it != se.end(); ++it) { cout << *it << " ";}

3.2 存储自定义数据类型

Set可以存储自定义数据类型的对象,例如自定义的Point结构体。

#include 
#include
#include
#include
struct Point { int x, y; bool operator<(const Point& rhs) const { if (x == rhs.x) { return y < rhs.y; } else { return x < rhs.x; } }};set
v;for (int i = 0; i < n; ++i) { Point temp; cin >> temp.x >> temp.y; v.insert(temp);}for (set
::iterator it = v.begin(); it != v.end(); ++it) { cout << it->x << " " << it->y << endl;}

4. 栈 - Stack

Stack是一个先进后出(FILO)的数据结构,常用于操作顺序。

4.1 基本操作

  • 推入:使用push方法将元素推入栈顶。
  • 弹出:使用pop方法从栈顶取出元素。
  • 查看顶部:使用top方法查看栈顶元素。
#include 
#include
using namespace std;stack
s;s.push(1);s.push(2);s.push(3);cout << s.top() << endl;s.pop();

5. 队列 - Queue

Queue是一个先进先出(FIFO)的数据结构,常用于任务队列。

5.1 基本操作

  • 入队:使用push方法将元素入队。
  • 出队:使用frontback方法访问队首和队尾元素,pop方法取出队首元素。
  • 遍历:使用迭代器逐个访问队列中的元素。
#include 
#include
using namespace std;queue
que;que.push(1);que.push(2);que.push(3);cout << que.front() << endl;que.pop();

6. 优先队列 - Priority_queue

Priority_queue是一种优先队列,支持根据指定的比较函数进行元素的优先级排序。

6.1 基本操作

  • 构建:使用priority_queue模板构造对象,传入比较函数。
  • 入队:使用push方法入队。
  • 出队:使用top方法查看队首元素,pop方法取出队首元素。
#include 
#include
#include
using namespace std;priority_queue
, greater
> q;int n, x;cin >> n >> x;for (int i = 0; i < n; ++i) { cin >> x; q.push(x);}cout << q.top() << endl;

7. 映射 - Map

Map是一个双向映射,键和值都可以是任意类型,键集自动排序。

7.1 基本操作

  • 插入:使用insert方法插入键值对。
  • 查找:使用operator[]方法直接访问或查找键值对。
  • 删除:使用erase方法删除键值对。
  • 遍历:使用迭代器逐个访问键值对。
#include #include 
#include
#include
using namespace std;map
dict;dict.insert(map
::pair("Tom", 1));dict.insert(map
::pair("Aam", 2));dict.insert(map
::pair("IlS", 3));dict["Bia"] = 5;dict["Uia"] = 3;if (dict.count("Tom")) { cout << "Tom is " << dict["Tom"] << endl;} else { cout << "not find Tom" << endl;}for (map
::iterator it = dict.begin(); it != dict.end(); ++it) { cout << it->first << " " << it->second << endl;}

8. STL练习

8.1 锯齿数组

根据输入的位置和值,向对应行插入数据。

#include 
#include
using namespace std;vector
> mat(10005);int n, m, x, y;cin >> n >> m;for (int i = 0; i < m; ++i) { cin >> x >> y; mat[x].push_back(y);}for (int i = 1; i <= n; ++i) { for (int j = 0; j < mat[i].size(); ++j) { cout << mat[i][j] << " "; } cout << endl;}

8.2 蒜头君堆积木问题

根据输入的木块大小,完成堆积木的任务。

#include 
#include
using namespace std;vector
> v(10005);int n, m, a, b;cin >> n >> m;for (int i = 0; i < m; ++i) { cin >> a >> b; if (a == b) { continue; } for (int j = 0; j < v[b].size(); ++j) { v[a].push_back(v[b][j]); } v[b].swap();}

8.3 蒜头君的水果店

根据输入的销售记录,统计每个类别的销售量。

#include #include 
using namespace std;map
> mp;int n;cin >> n;for (int i = 0; i < n; ++i) { string s1, s2; int d; cin >> s1 >> s2 >> d; mp[s2][s1] += d;}for (map
>::iterator it1 = mp.begin(); it1 != mp.end(); ++it1) { cout << it1->first << endl; for (map
::iterator it2 = it1->second.begin(); it2 != it1->second.end(); ++it2) { cout << " |----" << it2->first << " (" << it2->second << ")" << endl; }}

通过以上内容,可以看到各个STL容器的基本用法和实际应用场景。

上一篇:oracle入门基础(3) 简单的sql及一些注意事项
下一篇:Leetcode--24. 两两交换链表中的结点

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2026年06月13日 03时35分33秒