标准容器map的使用(创建优先级队列)
#ifndef __STD_LIST_TEST__#define __STD_LIST_TEST__#include#include#includeclass TestData{public:TestData(){m_Num = 0;}public:int m_Num;};#endif#include "St
·
#ifndef __STD_LIST_TEST__
#define __STD_LIST_TEST__
#include <list>
#include <map>
#include <stdio.h>
class TestData
{
public:
TestData()
{
m_Num = 0;
}
public:
int m_Num;
};
#endif
#include "StdListTest.h"
#define TESTLIST list<TestData*>
#define TESTMAP map< int, list<TestData*> >
#define NUM 10
using namespace std;
int main(int argc, char* argv[])
{
int size = 0;
TESTLIST TestList;
TESTMAP TestMap;
printf("开始向map里压入10个队列...\n");
TESTLIST::iterator iterList = TestList.begin();
TESTMAP::iterator iterMap = TestMap.begin();
for (int j = 0; j < NUM; j++)
{
for (int i = j; i < NUM; i++)
{
TestData* test = new TestData;
test->m_Num = i;
TestList.push_back(test);
}
iterMap = TestMap.find(j);
if (iterMap == TestMap.end())
{
TestMap[j] = TestList;
while(iterList != TestList.end())
{
TestData* m_Temp = TestList.front();
delete m_Temp;
m_Temp = NULL;
iterList = TestList.erase(iterList);
}
TestList.clear();
}
}
printf("开始访问map里的数据,然后删除[%d]\n", TestMap.size());
iterMap = TestMap.begin();
while (iterMap != TestMap.end())
{
iterList = iterMap->second.begin();
printf("优先级[%d]: ", iterMap->first);
while (iterList != iterMap->second.end())
{
printf("[%d] ", (*iterList)->m_Num);
iterList = TestList.erase(iterList);
}
printf("\n");
TestMap.erase(iterMap++);//此处与list不一样,删除后不能返回地址
}
printf("map删除后空间[%d]\n", TestMap.size());
return 0;
}
更多推荐
已为社区贡献1条内容
所有评论(0)