第十五周项目:范型程序阅读

/*
 *Copyright(c)2016.烟台大学计算机学院
 *All right reserved.
 *文件名称:test.cpp
 *作者:黄金婵
 *完成日期:2016年6月24日
 *版本号:v1.0
 *
 *问题描述:
 *程序输入:
 *程序输出:
 */

#include <algorithm>
#include<map>
#include<iterator>
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
    map<char,int> mymap;
    mymap['a']=10;
    mymap['b']=60;
    mymap['c']=30;
    mymap['d']=90;
    mymap['e']=50;

    map<char,int> second(mymap);
    map<char,int> third(mymap.begin(),mymap.end());
    map<char,int>::key_compare key_comp;
    map<char,int>::iterator it;
    it=mymap.begin();
    for (;it!=mymap.end();it++)
    {
        cout<<it->first<<":"<<it->second<<endl;
    }
    cout<<"================================="<<endl;
    second.clear();
    second['a']=1002;
    second['b']=10023;
    while (!second.empty())
    {
        cout << second.begin()->first << " => ";
        cout << second.begin()->second << endl;
        second.erase(second.begin());
    }
    cout<<"================================="<<endl;
    mymap.insert(pair<char,int>('f',100) );
    mymap.insert(pair<char,int>('g',200) );
    cout<<"f => " <<mymap.find('f')->second<<endl;
    cout<<"g => " <<mymap.find('g')->second<<endl;

    cout<<"================================="<<endl;
    key_comp=mymap.key_comp();
    cout << "mymap contains:\n";

    char highest=mymap.rbegin()->first;     // key value of last element

    it=mymap.begin();
    do {
        cout << (*it).first << " => " << (*it).second << endl;
    } while ( key_comp((*it++).first, highest) );

    cout << endl;
    return 0;
}


知识点总结:


这里我们将体会,map在数据插入时保证有序的好处。要判定一个数据(关键字)是否在map中出现的方法比较多,这里标题虽然是数据的查找,在这里将穿插着大量的map基本用法。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值