STL之map

特点:一对一数据,自动排序(由小到大)
头文件#include <map>
STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。

下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述,下面给出map描述代码:

Map<int, string> mapStudent;

map对象是模板类,需要关键字key和存储对象value两个模板参数,自动建立Key - value的对应。根据key值快速查找value进行操作。key 和 value可以是任意你需要的类型。

map<int, int>的元素是key-value,而first和second分别对应key和value,it->first返回当前元素的关键字,it->second返回当前元素的值。

这样理解:
定义MapForSort[99] = 1000000;
it->first就是下标,即99
it->second是值,即1000000

下面是一些简单基本使用的操作 参考博客
1.map最基本的6个构造函数

map<string , int >mapstring;         map<int ,string >mapint;
   map<sring, char>mapstring;         map< char ,string>mapchar;
   map<char ,int>mapchar;            map<int ,char >mapint;

2.map添加数据

map<int ,string> maplive;  
   1.maplive.insert(pair<int,string>(102,"aclive"));
   2.maplive.insert(map<int,string>::value_type(321,"hai"));
   3. maplive[112]="April";//map中最简单最常用的插入添加!

3.map中元素的查找
find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。

map<int,string >::iterator l_it;; 
   l_it=maplive.find(112);
   if(l_it==maplive.end())
       cout<<"we do not find 112"<<endl;
   else cout<<"wo find 112"<<endl;

4.map中元素的删除

如果删除112map<int ,string >::iterator l_it;;
   l_it=maplive.find(112);
   if(l_it==maplive.end())
        cout<<"we do not find 112"<<endl;
   else  maplive.erase(l_it);  //delete 112;

5.map中swap的使用
map中的swap不是一个容器中的元素交换,而是两个容器交换。
6.map中sort的使用
map中的元素是自动按key升序排序,所以不能对map用sort函数。
7.map的基本操作函数:
C++ Maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数

例题 数气球

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 and the second 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 blue
pink orange
想法:原题只让输出最大的,我给改了一下题意,输出最大的和次之的,map入门题,真正理解一对一的思想。

#include<iostream>
#include<map>
using namespace std;
int main()
{
    int n;
    string a;
    while(cin>>n&&n>0)
    {
        map<string, int>ss;
        while(n--)
        {
            cin>>a;
            ss[a]++;//每个颜色对应的次数增加
        }
        int max=0;
        string b;
        map<string, int>::iterator it;//迭代器,it就是一个指针
        for(it=ss.begin();it!=ss.end();it++)//遍历一遍
        {
            if(it->second>max)//次数的比较
            {
                max=(*it).second;//second对应value也就是这里的int次数
                b=(*it).first;//first对应key也就是这里的string
            }
        }
        int t=max;//用一个新的t来存储上轮遍历过的最大值
        max=0;//然后在重新遍历来求一次最大值
        string c;
        map<string, int>::iterator ip;//迭代器,ip就是一个指针
        for(ip=ss.begin();ip!=ss.end();ip++)//遍历一遍
        {
            if(ip->second<t&&ip->second>max)//!!!这里没有想出来!!!要大于除了max以外所有的!!!
            {
                max=(*ip).second;//second对应value也就是这里的int次数
                c=(*ip).first;//first对应key也就是这里的string
            }
        }
        cout<<b<< " "<<c<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值