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
原创粉丝点击