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

#include <time.h>
#include <stdlib.h>
#include <iostream.h>
#include <map.h>
#include <vector.h>
#include <algorithm>
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<m2.head.sequence ;
}

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

map<int,vector<m_message> *> ml;
map<int,vector<m_message>*>::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<m_message> vect;
nm = &vect;
ml.insert(map<int,vector<m_message>*>::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<m_message> vect;
nm = &vect;
ml.insert(map<int,vector<m_message>*>::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<m_message> vect;
nm = &vect;
ml.insert(map<int,vector<m_message>*>::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<m_message> vect;
nm = &vect;
ml.insert(map<int,vector<m_message>*>::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<m_message>.();


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<m_message>.();

return 0;
}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页