c++队列出现错误 在VS2015上面出现debug assertion failed

#include<iostream>
#include<cstdio>
#include<functional>
#include<algorithm>
#include<queue>
using namespace std;
queue <int > a;
int main()
{
int j = 0;
scanf_s("%d", &j);
for (int i = 0;i < j;i++)
{
int m = 0;
scanf_s("%d", &m);
a.push(m);
}
while (!a.empty())
{
cout<<a.front()<<" ";
a.pop();
int x = a.front();
a.push(x);
a.pop();
}
return 0;

}

代码如上

#include<iostream>
#include<cstdio>
#include<functional>
#include<algorithm>
#include<queue>
using namespace std;
queue <int> a;
int main()
{
int j = 0;
int m = 0;  // 把声明m的代码移出来,避免重复声明导致内存消耗

scanf_s("%d", &j);
for (int i = 0; i < j; i++)
{
scanf_s("%d", &m);
a.push(m);
}




while (!a.empty())
{
cout << a.front() << " ";  // 输出队列首元素
a.pop(); // 从队列里移除队头元素

// 注释掉下面有逻辑问题的代码即可正常运行。
//int x = a.front();
//a.push(x);
//a.pop();
}

system("pause");  // 这行是新加的,让屏幕暂停
return 0;

}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答