#include <iostream>
#include <queue>
using namespace std;
class T
{
public:
int x, y, z;
T(int a, int b, int c):x(a), y(b), z(c)
{
}
};
bool operator < (const T &t1, const T &t2)
{
return t1.z < t2.z; // 按照z 的顺序来决定t1 和t2 的顺序
}
main()
{
priority_queue<T> q;
q.push(T(4,4,3));
q.push(T(2,2,5));
q.push(T(1,5,4));
q.push(T(3,3,6));
while (!q.empty())
{
T t = q.top(); q.pop();
cout << t.x << " " << t.y << " " << t.z << endl;
}
return 1;
}
typedef struct
{
int a;
TCHAR* string;
}FILESTATE;
然后定一个全局的队列:
queue<FILESTATE> g_fileQueue;
然后在一个函数里面定义一个结构元素,并且为其string开辟内存并且赋值:
void Add(TCHAR* fileName)
{
FILESTATE fs;
int strLen;
if(fileName == NULL)
return;
strLen = _tcslen(fileName);
fs.string = new TCHAR[strLen+1]; //这里动态开辟一块内存
_tcscpy(fs.string, fileName);
*(fs.string+strLen+1) = '\0';
fs.a = 1; // 赋值操作,可以忽略~
g_fileQueue.push(fs);
}
然后在主函数中:
int _tmain(int argc, TCHAR* argv[])
{
TCHAR* str = "hahahahahahahahahahahahahahaha";
for(int i=0; i<100000000000; i++)
{
Add(str);
/*1*/ FILESTATE& fs = g_fileQueue.front();
/*2*/ g_fileQueue.pop();
/*3*/ delete[] fs.string; //此处出错,报告DAMAGE: after Normal block (*46) at 0xXXXXXX
}
return 1;
}