蓝桥杯算法练习笔记(5)__常用的STL
, greater > q;int n, x;cin >> n >> x;for (int i = 0; i < n; ++i) { cin >> x; q.push(x);}cout << q.top() << endl;
发布日期: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方法将元素入队。 - 出队:使用
front和back方法访问队首和队尾元素,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
7. 映射 - Map
Map是一个双向映射,键和值都可以是任意类型,键集自动排序。
7.1 基本操作
- 插入:使用
insert方法插入键值对。 - 查找:使用
operator[]方法直接访问或查找键值对。 - 删除:使用
erase方法删除键值对。 - 遍历:使用迭代器逐个访问键值对。
#include
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
通过以上内容,可以看到各个STL容器的基本用法和实际应用场景。
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2026年06月13日 03时35分33秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
php对象引用和析构函数的关系
2023-03-01
RabbitMQ HTTP 认证后端项目常见问题解决方案
2023-03-01
PHP将图片转换成base64格式(优缺点)
2023-03-01
php将多个值的数组去除重复元素
2023-03-01
php局域网上传文件_PHP如何通过CURL上传文件
2023-03-01
PHP工具插件大全
2023-03-01
php布尔值的++
2023-03-01
PHP常量、变量作用域详解(一)
2023-03-01
PHP应用目录结构设计
2023-03-01
PHP应用程序连接MSQL数据库Demo(附crud程序)
2023-03-01
PHP应用程序连接Oracle数据库Demo(附Oracle客户端安装文件)
2023-03-01
PHP开发api接口安全验证
2023-03-01
PHP开发规范PSR
2023-03-01
PHP开发遇到错误0001
2023-03-01
php异常处理
2023-03-01
PHP引入了泛型和集合两大重要特性,大大改善 PHP 代码的可维护性和可读性
2023-03-01
PHP引擎php.ini参数优化
2023-03-01
PHP引用(&)使用详解
2023-03-01
php引用及垃圾回收
2023-03-01
php当前时间的集中写法
2023-03-01