题目链接:

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的更多相关文章

  1. 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, ...

  2. 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 ...

  3. Gunner II--hdu5233(map&vector/二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 ...

  4. 几种常见 容器 比较和分析 hashmap, map, vector, list ...hash table

    list支持快速的插入和删除,但是查找费时; vector支持快速的查找,但是插入费时. map查找的时间复杂度是对数的,这几乎是最快的,hash也是对数的.  如果我自己写,我也会用二叉检索树,它在 ...

  5. 2018.09.26 洛谷P2464 [SDOI2008]郁闷的小J(map+vector)

    传送门 本来出题人出出来想考数据结构的. 但是我们拥有map+vector/set这样优秀的STL,因此直接用map离散化,vector存下标在里面二分找答案就行了. 代码: #include< ...

  6. [知识点]C++中STL容器之map

    UPDATE(20190416):写完vector和set之后,发现不少内容全部引导到map上了……于是进行了一定的描述补充与更正. 零.STL目录 1.容器之map 2.容器之vector 3.容器 ...

  7. 标准模板库(STL)学习探究之vector容器

    标准模板库(STL)学习探究之vector容器  C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...

  8. 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 ...

  9. map,vector 等容器内容的循环删除问题(C++)

    map,vector 等容器内容的循环删除问题(C++) map,vector等容器的循环删除不能用普通的方法删除: for(auto p=list.begin();p!=list.end();p++ ...

随机推荐

  1. The Preliminary Contest for ICPC China Nanchang National Invitational and International Silk-Road Programming Contest

    打网络赛 比赛前的准备工作要做好 确保 c++/java/python的编译器能用 打好模板,放在桌面 A. PERFECT NUMBER PROBLEM #include <cstdio> ...

  2. GWAS群体分层 (Population stratification):利用plink对基因型进行PCA

    一.为什么要做祖先成分的PCA? GWAS研究时经常碰到群体分层的现象,即该群体的祖先来源多样性,我们知道的,不同群体SNP频率不一样,导致后面做关联分析的时候可能出现假阳性位点(不一定是显著信号位点 ...

  3. server被强制关闭,

    一个client和一个Server,两者之间建立了一个基于TCP的socket连接,在刚刚建立好连接后,尚未进行数据传输,Server端应用程序突然crush掉了,现在立刻重启Server端应用程序( ...

  4. elk插件以及分词器安装

    ElasticSearch-Head 安装配置因为安装 ElasticSearch-Head 需要使用到 npm 包管理器,所以需要我们提前安装好 NodeJS ,安装 NodeJS 的方法可以参考: ...

  5. Docker:网络及数据卷设置 [四]

    一.Docker网络设置 默认情况下,docker会创建一个桥接网卡[docker 0],docker有2种映射方式,一种是随机映射,一种是指定映射 提示:生产场景一般不使用随机映射,但是随机映射的好 ...

  6. C++回顾day03---<多态>

    一:错误理解下的多态 #include <iostream> using namespace std; class Parent { public: Parent() { cout < ...

  7. 常用的消息队列中间件mq对比

    原文地址:https://blog.csdn.net/qq_30764991/article/details/80239076 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量 ...

  8. SSH框架之hibernate《二》

    Hibernate第二天     一.hibernate的持久化类和对象标识符         1.1持久化类的编写规范             1.1.1什么是持久化类:               ...

  9. Coroutine的原理以及实现

    最近在写WinForm,在UI界面需要用到异步的操作,比如加载数据的同时刷系进度条,WinForm提供了不少多线程的操作, 但是多线程里,无法直接修改主线程里添加的UI的get/set属性访问器(可以 ...

  10. 【Unity]】AR小工具-Vuforia

    很有意思的增强现实玩具,六分钟应用. https://www.youtube.com/watch?v=khavGQ7Dy3c