S - stl 的mapⅠ
先来介绍一下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
This year, they decide to leave this lovely job to you.
Input
A test case with N = 0 terminates the input and this test case is not to be processed.
Output
Sample Input
Sample Output
#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Ⅰ的更多相关文章
- C++ STL中Map的按Key排序和按Value排序
map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...
- STL中map与hash_map的比较
1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O ...
- STL中map,set的基本用法示例
本文主要是使用了STL中德map和set两个容器,使用了它们本身的一些功能函数(包括迭代器),介绍了它们的基本使用方式,是一个使用熟悉的过程. map的基本使用: #include "std ...
- STL中map与hash_map容器的选择收藏
这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...
- C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET
C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...
- STL之map排序
描述 STL的map中存储了字符串以及对应出现的次数,请分别根据字符串顺序从小到大排序和出现次数从小到大排序. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { ...
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- C++ STL中Map的按Key排序跟按Value排序
C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...
- [STL] Implement "map", "set"
练习热身 Ref: STL中map的数据结构 C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Re ...
- stl中map的四种插入方法总结
stl中map的四种插入方法总结方法一:pair例:map<int, string> mp;mp.insert(pair<int,string>(1,"aaaaa&q ...
随机推荐
- 某Java游戏服务器用到的知识
Runtime.getRuntime().addShutdownHook(shutdownHook); shutdownHook()函数可以在jvm关闭的时候进行内存清理.对象销毁等操作 http:/ ...
- 8051、ARM、AVR
arm精简指令集.可以安装嵌入式操作系统 8051复杂指令集 avr没落了
- stretchlim函数分析
在看imadjust代码时,看到stretchlim函数,特此分析一下,代码注释如下 function lowhigh = stretchlim(varargin) %STRETCHLIM Find ...
- 17款提高编程效率的css工具
参看下面链接:<17款提高编程效率的css工具>
- Hibernate之通过hibernate.cfg.xml配置文件访问数据库的例子
hibernate.cfg.xml文件内容: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- jQuery中的$.extend方法总结
原文见:jQuery.extend()函数详解 Jquery的扩展方法extend是我们在写插件的过程中常用的方法,但是经常容易搞不清楚以下两个写法的关系: 1.$.extend(dest,src1, ...
- Ecstore 2.0 报表显示空白
INSERT INTO `sdb_ectools_analysis` (`id`, `service`, `interval`, `modify`) VALUES (, ), (, ), (, ); ...
- python Post方式发起http请求 使用百度接口地理编码
import os import httplib import json import urllib baiduapi="api.map.baidu.com:80" src=&qu ...
- UFI命令格式里SCSI指令
有三种字长命令:6位.10位.12位,一般Windows下用12位. 在UFI 命令格式里SCSI指令用到如下: 指令代码 指令名称 说明 04h Format Unit 格式化存储单元 12h In ...
- Jedis超时时间设置梳理
JedisConnectionException: Unexpected end of stream #932 Repeatable exception and for the life of me, ...