先来介绍一下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. 关于Ajax技术中servlet末尾的输出流

    Ajax的服务器端用PrintWriter out=resp.getWriter()来响应数据的时候,out.print(0).out.print(1)来表示成功或失败,而不用out.write是有原 ...

  2. BZOJ 2176 Strange String (最小表示法)

    题目大意: 与别的裸题的唯一不同点是其符号的ASCII码值在3 ~ 254 之间. 算法讨论: 最小表示法直接上.但是唯一不同的就是注意这里的字符范围,用char是会get wa的,所以要用unsig ...

  3. [hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂. 由于今天下 ...

  4. python调win32api调整屏幕分辨率

    需要频繁切换屏幕分辨率,想写个脚本来实现,需要切换时运行一下就好 在网上查到,需要用windows的api,ChangeDisplaySettings 实现代码如下 import win32api d ...

  5. [c language] getopt

    getopt(分析命令行参数)     相关函数表头文件         #include<unistd.h>定义函数         int getopt(int argc,char * ...

  6. C语言函数入门

    由于采用了函数模块式的结构,C语言易于实现结构化程序设计.使程序的层次结构清晰,便于程序的编写.阅读.调试. main 函数是主函数,它可以调用其它函数,而不允许被其它函数调用.因此,C程序的执行总是 ...

  7. Sumsets(POJ 2229 DP)

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 15293   Accepted: 6073 Descrip ...

  8. ZOJ 3745 Salary Increasing

    Description Edward has established a company with n staffs. He is such a kind man that he did Q time ...

  9. 基于stm32f103zet6的FAT16文件系统学习1(初识FAT16)

    有了之前读写block的基础之后,准备弄个文件系统,之前没有接触过这东西,所以有很多都晕晕的,但是看到fat的源代码之后还是挺有信心的,因为之前一直过uboot,所以这个文件当然是小巫见大巫了.首先来 ...

  10. 【温故而知新-万花筒】C# 异步编程 逆变 协变 委托 事件 事件参数 迭代 线程、多线程、线程池、后台线程

    额基本脱离了2.0 3.5的时代了.在.net 4.0+ 时代.一切都是辣么简单! 参考文档: http://www.cnblogs.com/linzheng/archive/2012/04/11/2 ...