map类的实现代码

template<class _Kty,class _Ty,class _Pr = less<_Kty>,class _Alloc = allocator<pair<const _Kty, _Ty> > >class map: public _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, false> >{  // ordered red-black tree of {key, mapped} values, unique keys
public:typedef map<_Kty, _Ty, _Pr, _Alloc> _Myt;typedef _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, false> > _Mybase;typedef _Kty key_type;typedef _Ty mapped_type;typedef _Ty referent_type;  // retainedtypedef _Pr key_compare;typedef typename _Mybase::value_compare value_compare;typedef typename _Mybase::allocator_type allocator_type;typedef typename _Mybase::size_type size_type;typedef typename _Mybase::difference_type difference_type;typedef typename _Mybase::pointer pointer;typedef typename _Mybase::const_pointer const_pointer;typedef typename _Mybase::reference reference;typedef typename _Mybase::const_reference const_reference;typedef typename _Mybase::iterator iterator;typedef typename _Mybase::const_iterator const_iterator;typedef typename _Mybase::reverse_iterator reverse_iterator;typedef typename _Mybase::const_reverse_iteratorconst_reverse_iterator;typedef typename _Mybase::value_type value_type;map(): _Mybase(key_compare(), allocator_type()){    // construct empty map from defaults}map(const _Myt& _Right): _Mybase(_Right){  // construct map by copying _Right}explicit map(const key_compare& _Pred): _Mybase(_Pred, allocator_type()){    // construct empty map from comparator}map(const key_compare& _Pred, const allocator_type& _Al): _Mybase(_Pred, _Al){   // construct empty map from comparator and allocator}template<class _Iter>map(_Iter _First, _Iter _Last): _Mybase(key_compare(), allocator_type()){   // construct map from [_First, _Last), defaultsthis->insert(_First, _Last);}template<class _Iter>map(_Iter _First, _Iter _Last,const key_compare& _Pred): _Mybase(_Pred, allocator_type()){    // construct map from [_First, _Last), comparatorthis->insert(_First, _Last);}template<class _Iter>map(_Iter _First, _Iter _Last,const key_compare& _Pred, const allocator_type& _Al): _Mybase(_Pred, _Al){    // construct map from [_First, _Last), comparator, and allocatorthis->insert(_First, _Last);}_Myt& operator=(const _Myt& _Right){   // assign by copying _Right_Mybase::operator=(_Right);return (*this);}map(_Myt&& _Right): _Mybase(_STD move(_Right)){  // construct map by moving _Right}_Myt& operator=(_Myt&& _Right){  // assign by moving _Right_Mybase::operator=(_STD move(_Right));return (*this);}mapped_type& operator[](key_type&& _Keyval){   // find element matching _Keyval or insert with default mappediterator _Where = this->lower_bound(_Keyval);if (_Where == this->end()|| this->comp(_Keyval, this->_Key(_Where._Mynode())))_Where = this->insert(_Where,_STD pair<key_type, mapped_type>(_STD move(_Keyval),mapped_type()));return ((*_Where).second);}void swap(_Myt& _Right){  // exchange contents with non-movable _Right_Mybase::swap(_Right);}void swap(_Myt&& _Right){    // exchange contents with movable _Right_Mybase::swap(_STD move(_Right));}#if _HAS_CPP0X#else /* _HAS_CPP0X */#if _HAS_STRICT_CONFORMANCEvoid erase(const_iterator _Where){ // erase element at _Where_Mybase::erase(_Where);}size_type erase(const key_type& _Keyval){ // erase and count all that match _Keyvalreturn (_Mybase::erase(_Keyval));}void erase(const_iterator _First, const_iterator _Last){ // erase [_First, _Last)_Mybase::erase(_First, _Last);}#endif /* _HAS_STRICT_CONFORMANCE */#endif /* _HAS_CPP0X */mapped_type& operator[](const key_type& _Keyval){ // find element matching _Keyval or insert with default mappediterator _Where = this->lower_bound(_Keyval);if (_Where == this->end()|| this->comp(_Keyval, this->_Key(_Where._Mynode())))_Where = this->insert(_Where,value_type(_Keyval, mapped_type()));return ((*_Where).second);}#if _HAS_CPP0Xmapped_type& at(const key_type& _Keyval){ // find element matching _Keyvaliterator _Where = this->lower_bound(_Keyval);if (_Where == this->end()|| this->comp(_Keyval, this->_Key(_Where._Mynode())))_Xout_of_range("invalid map<K, T> key");return ((*_Where).second);}const mapped_type& at(const key_type& _Keyval) const{  // find element matching _Keyvalconst_iterator _Where = this->lower_bound(_Keyval);if (_Where == this->end()|| this->comp(_Keyval, this->_Key(_Where._Mynode())))_Xout_of_range("invalid map<K, T> key");return ((*_Where).second);}#endif /* _HAS_CPP0X */}

std::map实际应用

 OutputDebugString("*************************************************************************\n");OutputDebugString("*************************************************************************\n");OutputDebugString("*************************************************************************\n");OutputDebugString("*************************************************************************\n");// 任务ID  根据任务创建实验的IDtypedef std::map<int, int>           IDMAP;typedef std::map<int, int>::iterator IDMAPPt;typedef std::pair<int, int>          IDMAPPair;IDMAP   *idMap = new IDMAP;for(int i=1;i<=10;i++){idMap->insert(IDMAPPair(i,i*10));}for (IDMAPPt it = idMap->begin(); it != idMap->end(); it++){it->first;it->second;CString str ;str.Format("%7d%7d\n",it->first,it->second);OutputDebugString(str);}OutputDebugString("*************************************************************************\n");IDMAPPt it;it = idMap->begin();while(it != idMap->end()){if(it->first == 5){// 查找指定元素的位置IDMAPPt pos = idMap->find(it->first);// 删除指定位置的元素idMap->erase(pos);int i=0;// 获取容器的大小i = idMap->size();if(i>0){// 将ptr定位到容器的开始it = idMap->begin();}continue;}it->first;it->second;CString str ;str.Format("%7d%7d\n",it->first,it->second);OutputDebugString(str);it++;}OutputDebugString("*************************************************************************\n");for (IDMAPPt it = idMap->begin(); it != idMap->end(); it++){it->first;it->second;CString str ;str.Format("%7d%7d\n",it->first,it->second);OutputDebugString(str);}
</pre><pre>

输出结果

*************************************************************************
*************************************************************************
*************************************************************************
*************************************************************************1     102     203     304     405     506     607     708     809     9010    100
*************************************************************************1     102     203     304     401     102     203     304     406     607     708     809     9010    100
*************************************************************************1     102     203     304     406     607     708     809     9010    100

std::map的使用相关推荐

  1. std::map只修改不用加锁

    std::map创建并初始化之后,不会再有任何的insert和delete键值对的操作.仅有的操作就是不同的线程会访问不同的key,对key对应的value进行修改,请问这种情况下,是否需要加锁? 不 ...

  2. std::map char*做key

    用 char*作为std::map中的key 网上的用法: 声明map时需要添加一个cmp比较函数,不然map在比较时,使用char *的指针进行比较,而不是比较char字符串. [cpp] view ...

  3. 【c++】映射表std::map

    文章内容为网络搜集内容 std::map 映射表(Map)容器是一个按特定顺序存储以键值对组合而成的元素的关联容器 // <map> template < class Key,cla ...

  4. boost::spirit模块实现将由某个分隔符分隔的任意键/值对解析为 std::map的测试程序

    boost::spirit模块实现将由某个分隔符分隔的任意键/值对解析为 std::map的测试程序 实现功能 C++实现代码 实现功能 boost::spirit模块实现将由某个分隔符分隔的任意键/ ...

  5. P2814-家谱【图论,并查集,std map库】

    正题 题目链接: https://www.luogu.org/problemnew/show/P2814 大意 有n个父子关系(可能不止一个孩子),询问一些人最大的祖先 代码 #include< ...

  6. std::map用法总结

    给出了map的基本用法如插入.查找.删除.遍历等等,同时告诉你如何实现双键map,包括 (1) 只有两个键都匹配才命中目标 (2) 两个键中任意一个匹配就命中目标 可以扩展到多键 (一) 介绍特点:1 ...

  7. std::map用法

    std::map用法 STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用. 在STL模板类中,用于线性数据存储管理的类主要有vector, list, map ...

  8. 关于 std::set/std::map 的几个为什么

    2013-01-20 std::set/std::map (以下用 std::map 代表) 是常用的关联式容器,也是 ADT(抽象数据类型).也就是说,其接口(不是 OO 意义下的 interfac ...

  9. map std 浮点数索引_C std :: map持有任何类型的值

    Blindy的答案非常好(1),但只是为了完成答案:通过使用动态继承,还有另一种方法可以在没有库的情况下完成: class MyFieldInterface { int m_Size; // of c ...

最新文章

  1. JS深入--词法作用域、执行上下文与闭包
  2. oracle安装报错emca,求助:oracle 安装问题
  3. 全球及中国洗涤剂行业十四五总体规模与盈利状况分析报告2022版
  4. .net上传,一个选择直接上传(ashx)和byteArray上传
  5. matlab用解析法求二自由度阻尼系统的自由振动(先求系统状态方程
  6. mysql数据库命令_新手入门MYSQL数据库命令大全
  7. 淘宝中间件借阿里云对外 日均调千亿次
  8. 【JAVA】线程安全的案例
  9. [转] 在ASP.NET MVC3中使用EFCodeFirst 1.0
  10. Hudson poll scm 时间格式说明
  11. 射频功放学习之ADS原理图版图联合仿真
  12. 云计算与大数据概论(1) 云计算,大数据是什么
  13. ch10_ex32nbsp;荷兰国旗问题
  14. java计算机毕业设计快递配送平台源码+mysql数据库+系统+lw文档+部署
  15. 传智 刘意 2015年Java基础视频-深入浅出精华版 笔记 day24~day26(2016年4月26日13:11:30)
  16. 一个管道工的成功移民经历(转载)
  17. ssm基于Android的自来水收费系统APP-计算机毕业设计
  18. 2022年最新的Android面试大厂必考174题(附带详细答案)
  19. java面向对象(核心技术)
  20. 车载通信——车载网络故障诊断

热门文章

  1. python y轴倒转_python – Config Kivy y轴的反转输入
  2. 企业服务器上病房床号修改,关于医院病房安放陪护床(共享陪护床)申请报告...
  3. 排序算法java 简书_手撕算法---常见排序算法java实现
  4. 删除下拉框只找23火星软件_下拉推广选择23火星软件
  5. pip install keras_常用基本pip命令及报错问题解决(不断更新)
  6. linux内存管理(九)-缺页异常分析
  7. python处理数据集并制作词云图
  8. keras笔记(3)-猫狗数据集上的训练以及单张图片多张图片的测试
  9. 一道面试题引发的“血案”
  10. Hausaufgabe--Python 06