先来介绍一下stl中的map这个功能

头文件#include<map>

map是STL的一个关联容器,它提供一对一的数据处理能力

就像一个人对应一个编号一样

定义 为  map<int, string> mapPeople;

insert 插入数据,如下有三种插入数据的方法

#include <map>

#include <string>
#include <iostream>
using namespace std;
int main() { map<int, string> mapPeople;
mapStudent.insert(pair<int, string>(, "People_1"));
mapStudent.insert(pair<int, string>(, "People_2"));
mapStudent.insert(pair<int, string>(, "People_3"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){
cout<<iter->first<<" " <<iter->second<<endl;
}
// system("pause");
return ;
}

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main() {
map<int, string> mapPeople;
mapStudent.insert(map<int, string>::value_type (, "People_1"));
mapStudent.insert(map<int, string>::value_type (, "People_2"));
mapStudent.insert(map<int, string>::value_type (, "People_3"));
map<int, string>::iterator iter;
for(iter = mapPeople.begin(); iter != mapPeople.end(); iter++){
cout<<iter->first<<" "<<iter->second<<endl;
}
// system("pause");
return ;
}

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapPeople;
mapPeople[] = "People_1";
mapPeople[] = "People_2";
mapPeople[] = "People_3";
map<int, string>::iterator iter;
for(iter = mapPeople.begin(); iter != mapPeople.end(); iter++){
cout<<iter->first<<" "<<iter->second<<endl;
}
// system("pause");
return ;
}

利用size这个函数得知map的大小int n = mapPeople.size();

清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map

删除map中的指定元素:

mapTest.erase(keyValue);               //删除key==keyValue的所有元素

pos = mapTest.find(keyValue);        //移除第一个key==keyValue的元素
if (pos != mapTest.end())
{
mapTest.erase(pos);
}

Description

Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.        
This year, they decide to leave this lovely job to you.        
                

Input

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.        
A test case with N = 0 terminates the input and this test case is not to be processed.        
                

Output

For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.        
                

Sample Input

5
green
red
blue
red
red
3
pink
orange
pink
0
                

Sample Output

red
pink
 
 
#include<iostream>
#include<string>
#include <map>
using namespace std;
int main() {
int n;
string color;
map<string,int>ballon;
while(cin>>n&&n){
int p=;
string m="\0";
ballon.clear();
for(int i=;i<n;i++){
cin>>color;
ballon[color]++;
}
map<string,int>::iterator t;
for(t=ballon.begin();t!=ballon.end();t++){
if((*t).second>p){
p=(*t).second;
m=(*t).first;
}
}
cout<<m<<endl;
}
//system("pause");
return ;
}
 
 

S - stl 的mapⅠ的更多相关文章

  1. C++ STL中Map的按Key排序和按Value排序

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...

  2. STL中map与hash_map的比较

    1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O ...

  3. STL中map,set的基本用法示例

    本文主要是使用了STL中德map和set两个容器,使用了它们本身的一些功能函数(包括迭代器),介绍了它们的基本使用方式,是一个使用熟悉的过程. map的基本使用: #include "std ...

  4. STL中map与hash_map容器的选择收藏

    这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...

  5. C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET

    C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...

  6. STL之map排序

    描述 STL的map中存储了字符串以及对应出现的次数,请分别根据字符串顺序从小到大排序和出现次数从小到大排序. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { ...

  7. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  8. C++ STL中Map的按Key排序跟按Value排序

    C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...

  9. [STL] Implement "map", "set"

    练习热身 Ref: STL中map的数据结构 C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Re ...

  10. stl中map的四种插入方法总结

    stl中map的四种插入方法总结方法一:pair例:map<int, string> mp;mp.insert(pair<int,string>(1,"aaaaa&q ...

随机推荐

  1. Webfrom 生成流水号 组合查询 Repeater中单选与复选控件的使用 JS实战应用

                                             Default.aspx 网页界面 <%@ Page Language="C#" AutoE ...

  2. (原)ubuntu16中简单的使用google的protobuf

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5804395.html 参考网址: http://www.cnblogs.com/luosongchao ...

  3. 用Dockerfile构建docker image

    dockerfile是为快速构建docker image而设计的,当你使用docker build 命令的时候,docker 会读取当前目录下的命名为Dockerfile(首字母大写)的纯文本文件并执 ...

  4. WAMPSERVER2.2 无法启动的解决!

    转: PHP版本:5.3.10 XDEBG插件:php_xdebug-2.1.2-5.3-vc9.dll WAMPServer2.2用的是VC9编译的,并且需要VC9运行库支持. 此问题解决方法: 下 ...

  5. postgresql配置的一些问题

    ubuntu通过软件中心安装后,配置文件位于如下目录 我用超级用户创建了其它数据库用户,发现是登录不了的,必须还得创建同名的linux用户,甚是麻烦.在配置文件pg_hba.conf中发现了问题. 其 ...

  6. python----特性002

    python特性002:特性是可继承的 #!/usr/local/python3.5/bin/python3 class Person(object): def __init__(self,name) ...

  7. Java注释模板设置详解

    设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...

  8. Weblogic缓存

    缓存:如果脱离IDE工具的话,weblogic的文件会在\user_projects\domains\base_domain\servers\AdminServer\tmp\_WL_user文件夹中 ...

  9. Tea加密算法和XxTea加密算法

    TEA(Tiny Encryption Algorithm)是一种小型的对称加密解密算法,支持128位密码,与BlowFish一样TEA每次只能加密/解密8字节数据.TEA特点是速度快.效率高,实现也 ...

  10. QML的渲染方式相较于之前的版本也有了重大的更新(CPU线程负责绘制,GPU线程负责渲染),还有好多经常评论 good

    作者:qyvlik链接:http://www.zhihu.com/question/38867614/answer/78583440来源:知乎著作权归作者所有,转载请联系作者获得授权. 做UI啊.如果 ...