std::map的使用
来源:互联网 发布:无线网络破解软件 编辑:程序博客网 时间:2024/04/28 17:11
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 keyspublic: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 10 2 20 3 30 4 40 5 50 6 60 7 70 8 80 9 90 10 100************************************************************************* 1 10 2 20 3 30 4 40 1 10 2 20 3 30 4 40 6 60 7 70 8 80 9 90 10 100************************************************************************* 1 10 2 20 3 30 4 40 6 60 7 70 8 80 9 90 10 100
0 0
- std::map的使用
- std::map的使用
- std::map的使用
- std::map的使用例程
- std::map的简单使用例子
- C++中std::map的使用
- std::map 使用注意事项
- Std::map的排序
- std::map的用法
- std::map的机制
- std::map的使用方法
- 使用std::copy将std::map输出到std::cout
- c++ std::map使用注意
- std::map常见使用错误
- std::set/std::map的"建议插入"
- std::set/std::map 的几个为什么
- 高效的使用stl::map和std::set
- 使用std的vector,map,list, set等注意问题
- MySql函数大全
- Unity 2D 终结手册 (3) - 精灵编辑_Unity3d 开发技术
- Search for a Range 区间搜索
- Linux系统有线网络抓包程序
- Coursera机器学习 week5 神经网络的学习 assignment
- std::map的使用
- 剑指Offer:字符流中第一个不重复的字符
- 10、Foundation框架
- 为什么Android程序中的Looper.loop()不会造成ANR异常
- BZOJ4320 ShangHai2006 Homework
- [美剧赏析] 权力的游戏<Game of Thrones>完全赏析 (5-6)
- Unity3d导出安卓项目错误
- 黑马Android:获取sd卡剩余容量
- 记录一笔-数组