64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
multimap<string,string> mm;
mm.insert(make_pair(string("a"),string("a1")));
mm.insert(make_pair(string("a"),string("a2")));
mm.insert(make_pair(string("a"),string("a3")));
multimap<string,string>::size_type count=mm.count(string("a"));
multimap<string,string>::iterator iter=mm.find(string("a"));
for(multimap<string,string>::size_type i=0;i != count; ++i,++iter)
{
cout<<"iter->first : "<<iter->first<<'\t'<<"iter->second : "<<iter-
>second<<endl;
}
return 0;
}
typedef int key_t;
typedef int data_t;
typedef enum color_t
{
RED = 0,
BLACK = 1
}color_t;
typedef struct rb_node_t
{
struct rb_node_t *left, *right, *parent;
key_t key;
data_t data;
color_t color;
}rb_node_t;
// TEMPLATE CLASS multimap
template<class _Kty,
class _Ty,
class _Pr = less<_Kty>,
class _Alloc = allocator<pair<const _Kty, _Ty> > >
class multimap
: public _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, true> >
{ // ordered red-black tree of {key, mapped} values, non-unique keys
public:
...
};
// TEMPLATE CLASS _Tmap_traits
template<class _Kty, // key type
class _Ty, // mapped type
class _Pr, // comparator predicate type
class _Alloc, // actual allocator type (should be value allocator)
bool _Mfl> // true if multiple equivalent keys are permitted
class _Tmap_traits
: public _CONTAINER_BASE_AUX_ALLOC<_Alloc>
{ // traits required to make _Tree behave like a map
public:
...
};
protected:
key_compare comp;
_Pr comp; // the comparator predicate for keys
_Pairib insert(const value_type& _Val)
{ // try to insert node with value _Val
_Nodeptr _Trynode = _Root();
_Nodeptr _Wherenode = _Myhead;
bool _Addleft = true; // add to left of head if tree empty
while (!_Isnil(_Trynode))
{ // look for leaf to insert before (_Addleft) or after
_Wherenode = _Trynode;
_Addleft = _DEBUG_LT_PRED(this->comp,
this->_Kfn(_Val), _Key(_Trynode));
_Trynode = _Addleft ? _Left(_Trynode) : _Right(_Trynode);
}
if (this->_Multi)
return (_Pairib(_Insert(_Addleft, _Wherenode, _Val), true));
else
{ // insert only if unique
iterator _Where = _TREE_ITERATOR(_Wherenode);
if (!_Addleft)
; // need to test if insert after is okay
else if (_Where == begin())
return (_Pairib(_Insert(true, _Wherenode, _Val), true));
else
--_Where; // need to test if insert before is okay
if (_DEBUG_LT_PRED(this->comp,
_Key(_Where._Mynode()), this->_Kfn(_Val)))
return (_Pairib(_Insert(_Addleft, _Wherenode, _Val), true));
else
return (_Pairib(_Where, false));
}
}