STL复习之 map & vector --- disney HDU 2142
题目链接:
https://vjudge.net/problem/40913/origin
大致题意:
这是一道纯模拟题,不多说了。
思路:
map模拟,vector辅助
其中用了map的函数:
erase:
https://www.cnblogs.com/kex1n/archive/2011/12/06/2278505.html
上面这个博客的讲解非常透彻
大致的意思就是:
删除map内的一个节点。
比如我代码里的这句:
else if(op == "DELETE")
{
string name;
cin >> name;
it = mp.find(name);
if(it == mp.end())
{
printf("no such record\n");
}
else
{
mp.erase(it++);
printf("delete succeed\n");
}
}
这里就删除了it == mp.find(name)的节点。
要注意这里的
mp.erase(it++)
是避免出现程序无定义的行为
博客里写的很清楚。大致意思就是erase删除了这个节点,导致循环的时候会出现无节点的情况。这样it++就能跳过这个节点。 同样,我这段代码里也涉及到了map的find函数:
find函数用来定位数据出现位置,它返回的一个迭代器,。
当数据出现时,它返回数据所在位置的迭代器,
如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器!
所以我能用来判断是否存在name的。
it = mp.find(name);
关于vector我的代码用的不多,就是用作了一个string类的数组,这里主要用到了vector进行排序。
vector<string> name;
for(it = mp.begin(); it != mp.end(); ++it)
{
if(it->second == mx)
{
name.push_back(it->first);
}
}
//printf("%d %d\n", mx, name.size());
sort(name.begin(), name.end());
cout << mx << " " << name.size() << endl;
for(int i = ; i < name.size(); ++i) cout << name[i] << endl;
关于vector的运用这个博主写的很好
https://www.cnblogs.com/aiguona/p/7228364.html
大致的使用方法就是:
vector<int> v1
v1.push_back() //在数组的最后添加一个数据
v1.pop_back() //去掉数组的最后一个数据
v1.front() //返回第一个元素(栈顶元素)
v1.begin() //得到数组头的指针,用迭代器接受
v1.end() //得到数组的最后一个单元+1的指针,用迭代器接受
v1.clear() // 移除容器中所有数据
v1.empty() //判断容器是否为空
v1.erase(pos) //删除pos位置的数据
v1.erase(beg,end)// 删除[beg,end)区间的数据
v1.size() //回容器中实际数据的个数
v1.insert(pos,data) //在pos处插入数据 下面是AC代码:
#include <iostream>
#include <cstdio>
#include <string.h>
#include <map>
#include <vector>
#include <algorithm> using namespace std;
map<string, double> mp;
map<string, double> ::iterator it; //声明一个map的迭代器 int main()
{
string op;
while(cin >> op && op != "QUIT") //根据题意QUIT时退出
{
if(op == "NEW") //模拟NEW
{
double x;
string name;
cin >> name >> x;
if(mp[name]) //如果有存
{
mp[name] = x;
printf("update succeed\n");
}
else
{
mp[name] = x;
printf("A new record\n");
}
} else if(op == "MAX") //模拟max
{
if(mp.size() == ) //若map为空则输出0 0
{
printf("0 0\n");
continue;
}
double mx = mp.begin()->second;
for(it = mp.begin(); it != mp.end(); ++it)
{
mx = max(mx, it->second); //运用max函数找到最大值
}
vector<string> name;
for(it = mp.begin(); it != mp.end(); ++it)
{
if(it->second == mx)
{
name.push_back(it->first); //如果
}
}
cout << mx << ' ' << name.size() << endl;
for(int i = ; i < name.size(); ++i)
{
cout << name[i] << endl;
}
} else if(op == "AVERAGE")//模拟ACERAGE的情况
{
double sum = ;
if(mp.size() == )
{
printf("0.00\n");
continue;
}
for(it = mp.begin(); it != mp.end(); ++it)
{
sum += it->second;
}
printf("%0.2lf\n", sum/mp.size());
} else if(op == "DELETE")
{
string name;
cin >> name;
it = mp.find(name);
if(it == mp.end())
{
printf("no such record\n");
}
else
{
mp.erase(it++);
printf("delete succeed\n");
}
}
}
}
如有疑问,欢迎评论指出!
STL复习之 map & vector --- disney HDU 2142的更多相关文章
- UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)
Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...
- Gunner II--hdu5233(map&vector/二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 ...
- 几种常见 容器 比较和分析 hashmap, map, vector, list ...hash table
list支持快速的插入和删除,但是查找费时; vector支持快速的查找,但是插入费时. map查找的时间复杂度是对数的,这几乎是最快的,hash也是对数的. 如果我自己写,我也会用二叉检索树,它在 ...
- 2018.09.26 洛谷P2464 [SDOI2008]郁闷的小J(map+vector)
传送门 本来出题人出出来想考数据结构的. 但是我们拥有map+vector/set这样优秀的STL,因此直接用map离散化,vector存下标在里面二分找答案就行了. 代码: #include< ...
- [知识点]C++中STL容器之map
UPDATE(20190416):写完vector和set之后,发现不少内容全部引导到map上了……于是进行了一定的描述补充与更正. 零.STL目录 1.容器之map 2.容器之vector 3.容器 ...
- 标准模板库(STL)学习探究之vector容器
标准模板库(STL)学习探究之vector容器 C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...
- uva--11991 - Easy Problem from Rujia Liu?(sort+二分 map+vector vector)
11991 - Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for e ...
- map,vector 等容器内容的循环删除问题(C++)
map,vector 等容器内容的循环删除问题(C++) map,vector等容器的循环删除不能用普通的方法删除: for(auto p=list.begin();p!=list.end();p++ ...
随机推荐
- Vue(小案例_vue+axios仿手机app)_上拉加载
---恢复内容开始--- 一.前言 ...
- go 的匿名函数和闭包
匿名函数 匿名函数是指不需要定义函数名的一种函数实现方式. 在Go语言中,函数可以像普通变量一样被传递或使用,这与C语言的回调函数比较类似.不同的是,Go语言支持随时在代码里定义匿名函数. 匿名函数由 ...
- 第四节: Quartz.Net五大构件之Trigger通用用法(常用方法、优先级、与job关联等)
一. 简介 1. 几个类型: ①:TriggerBuilder:用来创建ITrigger实例 ②:ITrigger:触发器实例 2.常用的几个方法 ①.StartNow:Trigger马上触发. ②. ...
- [数学笔记Mathematical Notes]目录
2.也许是一个问题,暂时没给出解答. 2015年7月5日 1. 这个一个笔记类型的数学杂志, 打算用来记录自己学数学时做的笔记,一般几页纸一期. 觉得有意思就摘抄下来,或者自己的感想. 可能有些不是原 ...
- python中字符串编码转换
字符串编码转换程序员最苦逼的地方,什么乱码之类的几乎都是由汉字引起的. 其实编码问题很好搞定,只要记住一点: 任何平台的任何编码,都能和Unicode互相转换. UTF-8与GBK互相转换,那就先把U ...
- linux下 vi命令编辑/etc/my.cnf
把my.cnf配置文件加个max_connections包括(插入命令,删除命令,修改命令.退出保存命令) 你要有这个文件写权限,shell下输入: vi /etc/my.cnf 进入vi后,按i移动 ...
- mysql 备份报错mysqldump: [Warning] Using a password on the command line interface can be insecure.
-------------------------------------------------------------------------------- mysql 备份报错mysqldump ...
- mysql官方测试库
sql语句优化时没有测试数据,oracle官方提供测试数据 https://dev.mysql.com/doc/employee/en/employees-installation.html 到 ht ...
- 【原创】数据库基础之Mysql(2)主从库配置
一 安装 # wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm# yum -y insta ...
- Elasticsearch 安装和配置
1. 下载并解压 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.0.tar.gz ...