map类的实现代码
template,
class _Alloc = allocator > >
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; // retained
typedef _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_iterator
const_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
map(_Iter _First, _Iter _Last)
: _Mybase(key_compare(), allocator_type())
{ // construct map from [_First, _Last), defaults
this->insert(_First, _Last);
}
template
map(_Iter _First, _Iter _Last,
const key_compare& _Pred)
: _Mybase(_Pred, allocator_type())
{ // construct map from [_First, _Last), comparator
this->insert(_First, _Last);
}
template
map(_Iter _First, _Iter _Last,
const key_compare& _Pred, const allocator_type& _Al)
: _Mybase(_Pred, _Al)
{ // construct map from [_First, _Last), comparator, and allocator
this->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 mapped
iterator _Where = this->lower_bound(_Keyval);
if (_Where == this->end()
|| this->comp(_Keyval, this->_Key(_Where._Mynode())))
_Where = this->insert(_Where,
_STD pair(
_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_CONFORMANCE
void erase(const_iterator _Where)
{ // erase element at _Where
_Mybase::erase(_Where);
}
size_type erase(const key_type& _Keyval)
{ // erase and count all that match _Keyval
return (_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 mapped
iterator _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_CPP0X
mapped_type& at(const key_type& _Keyval)
{ // find element matching _Keyval
iterator _Where = this->lower_bound(_Keyval);
if (_Where == this->end()
|| this->comp(_Keyval, this->_Key(_Where._Mynode())))
_Xout_of_range("invalid map key");
return ((*_Where).second);
}
const mapped_type& at(const key_type& _Keyval) const
{ // find element matching _Keyval
const_iterator _Where = this->lower_bound(_Keyval);
if (_Where == this->end()
|| this->comp(_Keyval, this->_Key(_Where._Mynode())))
_Xout_of_range("invalid map key");
return ((*_Where).second);
}
#endif /* _HAS_CPP0X */
}
OutputDebugString("*************************************************************************\n");
OutputDebugString("*************************************************************************\n");
OutputDebugString("*************************************************************************\n");
OutputDebugString("*************************************************************************\n");
// 任务ID 根据任务创建实验的ID
typedef std::map IDMAP;
typedef std::map::iterator IDMAPPt;
typedef std::pair 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);
}
输出结果
*************************************************************************
*************************************************************************
*************************************************************************
*************************************************************************
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