——一个华科大差生的12年程序员工作总结

相关博文:《Essential C++》笔记之关联容器map的使用总结
相关博文:C++《STL和泛型编程》容器不带/带有成员函数总结

map和multimap结构



一.map和multimap的构造函数和析构函数

二.map和multimap的非更易型操作

三.map和multimap的查找操作函数

四.map和multimap的赋值操作

五.map和multimap的迭代器相关函数

六.map和multimap的安插和移除

七.map的直接元素访问操作

八.map和multimap运用实例

例1:在map/multimap身上使用算法和Lambda(本程序示例摘自《C++标准库》(第二版)第7.8.5小节)


附例1代码:

#include<map>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;int main()
{map<string,double>coll{{"tim",9.9},{"struppi",11.77}};//square the value of each element:for_each(coll.begin(),coll.end(),[](pair<const string,double>& elem){elem.second*=elem.second;});//printf each elementfor_each(coll.begin(),coll.end(),[](const map<string,double>::value_type& elem){cout<<elem.first<<": "<<elem.second<<endl;});
}

例2:将map当作关联式数组(本程序示例摘自《C++标准库》(第二版)第7.8.5小节)

附例2代码:

#include<map>
#include<string>
#include<iostream>
#include<iomanip>
using namespace std;int main()
{//create map/associative array//-keys are strings//-values are floatstypedef map<string,float>StringFloatMap;StringFloatMap stocks;//create empty container//insert some elementsstocks["BASF"]=369.50;stocks["VW"]=413.50;stocks["Daimler"]=819.00;stocks["BMW"]=834.00;stocks["Siemens"]=842.20;//print all elementsStringFloatMap::iterator pos;cout<<left;//left-adjust-valuesfor(pos=stocks.begin(); pos!=stocks.end(); ++pos) {cout<<"stocks: "<<setw(12)<<pos->first<<"price: "<<pos->second<<endl;}cout<<endl;//boom(all prices doubled)for(pos=stocks.begin(); pos!=stocks.end(); ++pos) {pos->second*=2;}//print all elementsfor(pos=stocks.begin(); pos!=stocks.end(); ++pos) {cout<<"stocks: "<<setw(12)<<pos->first<<"price: "<<pos->second<<endl;}cout<<endl;//rename key from "VM" to "Volkswagen"//-provided only by exchanging elementstocks["Volkswagen"]=stocks["VW"];stocks.erase("VW");//print all elementsfor(pos=stocks.begin(); pos!=stocks.end(); ++pos) {cout<<"stocks: "<<setw(12)<<pos->first<<"price: "<<pos->second<<endl;}
}

例3:将multimap当作字典(Dictionary)(本程序示例摘自《C++标准库》(第二版)第7.8.5小节)

《C++标准库》(第二版)第7.9.7小节)有一个相应的例子,使用无序(unordered)multimap做出字典(dictonary)。
附例3代码:

#include<map>
#include<string>
#include<iostream>
#include<iomanip>
using namespace std;int main()
{//create multimap as string/string dictionarymultimap<string,string> dict;//insert some elements in random orderdict.insert({{"day","Tag"},{"strange","fremd"},{"car","Auto"},{"smart","elegant"},{"trait","Merkmal"},{"strange","seltsam"},{"smart","raffiniert"},{"smart","klug"},{"clever","raffiniert"}});//print all elementscout.setf(ios::left,ios::adjustfield);cout<<' '<<setw(10)<<"english"<<"german"<<endl;cout<<setfill('-')<<setw(20)<<" "<<setfill(' ')<<endl;for(const auto& elem:dict){cout<<' '<<setw(10)<<elem.first<<elem.second<<endl;}cout<<endl;
//print all values for key "smart"string word("smart");cout<<word<<": "<<endl;for(auto pos=dict.lower_bound(word); pos!=dict.upper_bound(word); ++pos){cout<<" "<<pos->second<<endl;}//print all keys for value"raffiniert"word=("raffiniert");cout<<word<<": "<<endl;for(const auto& elem:dict){if(elem.second==word){cout<<" "<<elem.first<<endl;}}
}

例4:查找具有某特定Value的元素(本程序示例摘自《C++标准库》(第二版)第7.8.5小节)
这个例子示范了如何使用全局的find_if( )算法查找具有特定value的元素(对比于查找某特定key):

附例4代码:

#include<map>
#include<iostream>
#include<algorithm>
#include<utility>
using namespace std;int main()
{//map with floats as key and value_comp//-initilizing keys and values are automatically converted to floatmap<float,float>coll= {{1,7},{2,4},{3,2},{4,3},{5,6},{6,1},{7,3}};//search an element with key 3.0(logarithmic complexity)auto posKey=coll.find(3.0);if(posKey!=coll.end()){cout<<"key 3.0 found("<<posKey->first<<":"<<posKey->second<<")"<<endl;}//search an element with value 3.0(linear complexity)auto posVal=find_if(coll.begin(),coll.end(),[](const pair<float,float>&elem){return elem.second==3.0;});if(posVal!=coll.end()){cout<<"value 3.0 found("<<posVal->first<<":"<<posVal->second<<")"<<endl;}
}

九.综合实例:运用map、string并于运行期指定排序准则


例5:(本程序示例摘自《C++标准库》(第二版)第7.8.6小节)


运行结果:


附例5代码:

#include<map>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cctype>
using namespace std;//function object to compare strings
//~allows you to set the comparison criterion at runtime
//~allows you to compare case insensitive
class RuntimStringCmp
{public://constants for the comparison criterionenum cmp_mode{normal,nocase};
private://actual comparison modeconst cmp_mode mode;//auxiliary function to compare  case insensitivestatic bool nocase_compare(char c1,char c2){return toupper(c1)<toupper(c2);}
public://constructor:initializes the comparison criterionRuntimStringCmp(cmp_mode m=normal):mode(m) {}//the comparisonbool operator()(const string& s1,const string& s2)const{if(mode==normal){return s1<s2;}else{return lexicographical_compare(s1.begin(),s1.end(),s2.begin(),s2.end(),nocase_compare);}}
};//container type:
//-map with
//-string keys
//-string values
//-the special comparison object type
typedef map<string,string,RuntimStringCmp>StringStringMap;//function that fills and prints such containers
void fillAndPrint(StringStringMap& coll);int main()
{//create a container with the default comparison criterionStringStringMap coll1;fillAndPrint(coll1);//creat an object for case-insenstitive comparisonRuntimStringCmp ignorecase(RuntimStringCmp::nocase);//creat a container with the case-insensitive comparisons criterionStringStringMap coll2(ignorecase);fillAndPrint(coll2);
}void fillAndPrint(StringStringMap& coll)
{//insert elements in random ordercoll["Deutschland"]="Germany";coll["deutsch"]="German";coll["Haken"]="snag";coll["arbeiten"]="work";coll["Hund"]="dog";coll["gehen"]="go";coll["Unternehmen"]="enterprise";coll["unternehmen"]="undertake";coll["gehen"]="walk";coll["Bestatter"]="undertaker";//print elementscout.setf(ios::left,ios::adjustfield);for(const auto& elem:coll){cout<<setw(15)<<elem.first<<" "<<elem.second<<endl;}cout<<endl;
}

C++STL笔记(九):map和multimap详解相关推荐

  1. map和multimap 详解

    概况 Map和Multimap是将key/value pair 当做元素,进行管理.可以根据key的排序准则自动将元素排序.multimap允许重复元素,map不允许有重复,如图1所示. 图1 Map ...

  2. 读书笔记九:TCP/IP详解之广播和多播,IGMP协议

    单播,多播,广播的介绍 单播(unicast) 单播是说,对特定的主机进行数据传送.例如给某一个主机发送IP数据包.这时候,数据链路层给出的数据头里面是非常具体的目的地址,对于以太网来 说,就是具体网 ...

  3. C++STL笔记(七):forward list详解

    forward list结构 一.forward list的构造函数和析构函数 二.forward list的非更易型操作 三.forward list的赋值操作 四.forward list元素的直 ...

  4. c++STL容器的Map和multimap

    STL容器的Map和multimap map/multimap的简介 map/multimap对象的默认构造 map的插入与迭代器 迭代器遍历 map对象的拷贝构造与赋值 map的大小 map的删除 ...

  5. STL 中map的用法详解

    STL 中map的用法详解 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可 ...

  6. STL迭代器(iterator)用法详解

    C++ STL迭代器(iterator)用法详解 无论是序列容器还是关联容器,最常做的操作无疑是遍历容器中存储的元素,而实现此操作,多数情况会选用"迭代器(iterator)"来实 ...

  7. JDBC学习笔记02【ResultSet类详解、JDBC登录案例练习、PreparedStatement类详解】

    黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...

  8. ROS入门笔记(七):详解ROS文件系统

    ROS入门笔记(七):详解ROS文件系统 文章目录 01 Catkin编译系统 1.1 Catkin特点 1.2 Catkin工作原理 1.3 使用`catkin_make`进行编译 02 Catki ...

  9. Android进阶笔记:Messenger源码详解

    Messenger可以理解为一个是用于发送消息的一个类用法也很多,这里主要分析一下再跨进程的情况下Messenger的实现流程与源码分析.相信结合前面两篇关于aidl解析文章能够更好的对aidl有一个 ...

最新文章

  1. Educational Codeforces Round 50 (Rated for Div. 2)的A、B、C三题AC代码
  2. 存储过程学习笔记(SQL数据库
  3. atitit.Sealink2000国际海运信息管理系统
  4. java同名过滤器_Gateway Redis令牌桶请求限流过滤器
  5. Spring mvc DispatchServlet 实现机制
  6. 关于正则验证中文名字2-5位的时候
  7. 高 star 开源项目来实验楼啦,深度学习强推教材
  8. linux ifconfig route ip 网络相关命令
  9. mysql数据库5120_超傻瓜 H3C S5120 限速配置
  10. 随笔-机器如何学习我们的知识?
  11. java将Object对象转换成实体类对象
  12. dbms中怎么跨数据源拷贝数据_Oracle中使用DBMS_XPLAN处理执行计划详解
  13. java web网上书城_javaweb网上书城项目
  14. CTO 要我把这份 MySQL 规范贴在工位上!
  15. ActiveReports 9 新功能:创新的报表分层设计理念
  16. GCC编译器的安装教程(Windows环境)
  17. 德语翻译器在线翻译中文-德语翻译器支持各大语言翻译
  18. 源代码开发的公司该如何选择加密软件?
  19. day06三级缓存 二次采样
  20. 人工智能风暴狂飙突进,极致创新的微鲸深掘大屏金矿

热门文章

  1. js实现关于分页的一种实现方式
  2. thymeleaf中的th:remove用法
  3. Spring+springmvc+hibernate+redis整合配置文件
  4. react-native this使用笔记
  5. mysql中删除数据库中的表格数据恢复_恢复从数据库中删除的表
  6. 剑指offer:对称的二叉树(镜像,递归,非递归DFS栈+BFS队列)
  7. vue scss @font-face 路径问题
  8. django项目验证码(PIL库实现)
  9. 【读书笔记】【独立思考】2018-03-14
  10. 文件夹文件遍历并插入数据库的操作,IO Directory File的递归操作