C++ vector和map的使用(参考其他资料)

阅读更多

#include
#include
#include
#include
#include
#include
using namespace std;

#define MAX_BUF_SIZE 1024

typedef struct
{
short int length;
short int type;
int ID;
short int sequence;
short int flag;
}m_header;

typedef struct
{
m_header head;
char XmlMessages[MAX_BUF_SIZE];
}m_message;

////////////////////////////Vector排序函数的定义///////////////////////////
bool vectorCompare(const m_message & m1,const m_message & m2)
{
return m1.head.sequence}

int main()
{
m_message * mess;
vector* nm = NULL;

map *> ml;
map*>::iterator pIter;

m_message mess0;
mess=&mess0;
mess->head.length=4;
mess->head.type=802;
mess->head.sequence=1;
mess->head.ID = 2005;

for(pIter=ml.begin();pIter!=ml.end();pIter++)
{
if(pIter->first == mess->head.ID)
{nm = pIter->second;}
}
if(nm==NULL)
{
vector vect;
nm = &vect;
ml.insert(map*>::value_type(mess->head.ID,&vect));
}
(*nm).push_back(mess0);
nm = NULL;

m_message mess1;
mess=&mess1;
mess->head.length=4;
mess->head.type=801;
mess->head.sequence=2;
mess->head.ID = 2005;

for(pIter=ml.begin();pIter!=ml.end();pIter++)
{
if(pIter->first == mess->head.ID)
{nm = pIter->second;}
}
if(nm==NULL)
{
vector vect;
nm = &vect;
ml.insert(map*>::value_type(mess->head.ID,&vect));
}
(*nm).push_back(mess1);
nm = NULL;

m_message mess2;
mess=&mess2;
mess->head.length=4;
mess->head.type=801;
mess->head.sequence=3;
mess->head.ID = 2008;

for(pIter=ml.begin();pIter!=ml.end();pIter++)
{
if(pIter->first == mess->head.ID)
{nm = pIter->second;}
}
if(nm==NULL)
{
vector vect;
nm = &vect;
ml.insert(map*>::value_type(mess->head.ID,&vect));
}
(*nm).push_back(mess2);
nm = NULL;

m_message mess3;
mess=&mess3;
mess->head.length=4;
mess->head.type=801;
mess->head.sequence=2;
mess->head.ID = 2008;

for(pIter=ml.begin();pIter!=ml.end();pIter++)
{
if(pIter->first == mess->head.ID)
{nm = pIter->second;}
}
if(nm==NULL)
{
vector vect;
nm = &vect;
ml.insert(map*>::value_type(mess->head.ID,&vect));
}
(*nm).push_back(mess3);
nm = NULL;


for(pIter=ml.begin();pIter!=ml.end();pIter++)
{
if(pIter->first == 2005)
{
nm = pIter->second;
sort((*nm).begin(),(*nm).end(),vectorCompare);
}
}

for(int i=0;i<(*nm).size();i++)
{
printf("ID:%d,sequence:%d,type:%d\n",(&(*nm).at(i))->head.ID,(&(*nm).at(i))->head.sequence,(&(*nm).at(i))->head.type);
}

(*nm).~vector.();


for(pIter=ml.begin();pIter!=ml.end();pIter++)
{
if(pIter->first == 2008)
{
nm = pIter->second;
sort((*nm).begin(),(*nm).end(),vectorCompare);
}
}

for(int i=0;i<(*nm).size();i++)
{
printf("ID:%d,sequence:%d,type:%d\n",(&(*nm).at(i))->head.ID,(&(*nm).at(i))->head.sequence,(&(*nm).at(i))->head.type);
}

(*nm).~vector.();

return 0;
}

你可能感兴趣的:(C++Map,C++vector,Map,Vector)