i007.cc

i007.cc

优先队列-降维打击

multimap用法

multimap用法

multimap的特点为key是可以重复的,而普通map中的key是不可以重复的。
声明

multimap<int, CString>mapTest;
multimap<int, CString>::iterator pIter;
typedef multimap<int, CString>::iterator it;

插入,跟普通map相似

    mapTest.insert(PairTest(1, _T("a")));
    mapTest.insert(PairTest(1, _T("b")));
mapTest.insert(PairTest(1, _T("c")));
mapTest.insert(PairTest(2, _T("a")));

遍历,主要思路为根据key,multimap的特点为key是可以重复,即一个key对应多个value。将所有key取出来,然后每个key逐个遍历。

1取出所有的key,可以用set

    for (pIter = mapTest.begin(); pIter != mapTest.end(); pIter++)
{
    setKey.insert(pIter->first);
}

2.逐个key遍历

1
2
3
4
5
6
7
8
9
10
11
12
    pair<it, it> PairIter;
for (pIterKeySet = setKey.begin(); pIterKeySet != setKey.end(); pIterKeySet++)
{
    int iCurrentKey = *pIterKeySet;
    PairIter = mapTest.equal_range(iCurrentKey);
    while (PairIter.first != PairIter.second)
    {
        CString csTempKey = PairIter.first->second;
        cout<<csTempKey<<endl;
        PairIter.first++;       //这个要的
    }
}

这样就可以逐个遍历了

发表回复