std::map的使用


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;	// 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<class _Iter>
		map(_Iter _First, _Iter _Last)
		: _Mybase(key_compare(), allocator_type())
		{	// construct map from [_First, _Last), defaults
		this->insert(_First, _Last);
		}

	template<class _Iter>
		map(_Iter _First, _Iter _Last,
			const key_compare& _Pred)
		: _Mybase(_Pred, allocator_type())
		{	// construct map from [_First, _Last), comparator
		this->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 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<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_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<K, T> 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<K, T> key");
		return ((*_Where).second);
		}
 #endif /* _HAS_CPP0X */
	}

std::map实际应用

	OutputDebugString("*************************************************************************\n");
	OutputDebugString("*************************************************************************\n");
	OutputDebugString("*************************************************************************\n");
	OutputDebugString("*************************************************************************\n");

	// 任务ID  根据任务创建实验的ID
	typedef 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值