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

来源:互联网 发布:java入门教程 编辑:程序博客网 时间:2024/05/11 14:19
/* *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
原创粉丝点击