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 ...
随机推荐
- jsp页面获取服务器时间
Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MON ...
- 关于 knockout js 学习中的疑问 (1)
最近刚刚学习knockout中遇到如下问题: 1.在给viewModel定义一个方法时,有时后面跟 的this,有的时候没有 如下所示: this.fullName = ko.computed(fun ...
- ConcurrentQueue对列的基本使用方式
队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队. ConcurrentQueue< ...
- WP8.1 页面导航 缓存问题
最近开始学习wp8.1开发,在页面的导航学习时发现了一点问题,即当使用Frame.Navigate()方法进行页面的跳转时,每次都会重新实例化一个页面.而在新的页面采用Frame.GoBack()或者 ...
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ...
- C#读写word
操作word之前需要在COM引入Microsoft Office 12.0 Object Library(文件库可能不一样) 然后添加using Microsoft.Office.Interop.Wo ...
- 【Ecstore】为自建模块添加自定义主题模板
做好ECSOTRE模块后,需在主题中添加模板,而添加模板页面时只有产品分类页.产品详细页.首页等内置模块的模板类型. 下面介绍如何添加一个自定义的模板类型“buildings”. 一.修改(建议复制到 ...
- 深入 char * ,char ** ,char a[ ] ,char *a[] 内核
本文来自CSDN博客:daiyutage的文章 来源网页地址:http://blog.csdn.net/daiyutage/article/details/8604720 本人觉得这是一编很有价值的文 ...
- 编程修养-C语言篇(二)
1.版权和版本——————— 好的程序员会给自己的每个函数,每个文件,都注上版权和版本. 对于C/C++的文件,文件头应该有类似这样的注释: /*************************** ...
- cf471B MUH and Important Things
B. MUH and Important Things time limit per test 1 second memory limit per test 256 megabytes input s ...