如何清除stl的queue-CSDN论坛

2025-01-31 01:20:30
推荐回答(1个)
回答1:

typedef struct
{
int a;
TCHAR* string;
}FILESTATE;

然后定一个全局的队列:
queue 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;
}