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++ ...
随机推荐
- Notepad++ 的函数参数提示错误的问题终于解决了
看第3张图片,明明我输入的是 print_double(), 提示的却是 print() 函数的参数. 这个问题困扰了我半年,今天晚上找到解决问题的办法:
- 【Linux网络编程】TCP网络编程中connect()、listen()和accept()三者之间的关系
[Linux网络编程]TCP网络编程中connect().listen()和accept()三者之间的关系 基于 TCP 的网络编程开发分为服务器端和客户端两部分,常见的核心步骤和流程如下: conn ...
- ZooKeeper-客户端命令 zkCli
执行 bin/zkCli 文件进入客户端 查看帮助 help ZooKeeper -server host:port cmd args stat path [watch] set path data ...
- sublime中编译的sass如何改变css输出风格?【这里有答案】
由于在网上找了一遍没找到如果在sublime中将sass编译的css转换成为自己喜欢的风格,所以换了一种思路搜索到了答案,这里我将讲述如果更改. 首先sass总共有四种编译风格 (1) nested( ...
- 2018-2019-2 《Java程序设计》第4周学习总结
20175319 2018-2019-2 <Java程序设计>第4周学习总结 教材学习内容总结 第四周学习了如下内容: 子类与父类 子类的继承性 子类与对象 重写方法 super关键字 f ...
- [高中作文赏析]妈妈, 我心中的"灯"
- [数学笔记Mathematical Notes]1-调和级数发散的一个简单证明
定理. 调和级数 $\dps{\vsm{n}\frac{1}{n}}$ 是发散的. 证明. 设 $$\bex a_n=\sum_{k=1}^n\frac{1}{k}, \eex$$ 则 $a_n$ 递 ...
- Trie树的二三事QWQ
写在前面 Trie,又称字典树,是一种用于实现字符串快速检索的多叉树结构.Trie的每个结点都拥有若干字符指针,若在插入或检索字符串时扫描到一个字符c,就沿着当前节点的c这个字符指针,走向该指针指向的 ...
- excel合并
import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; impor ...
- day 15 - 2 内置函数练习
内置函数练习 编写 sql 查询语句功能 文件内容: 1,Eva,22,13651054608,IT2,Vera,23,13304320533,Tearcher3,Renault,25,1333235 ...