如果用erase()方法将队列deque中最后一个元素删除,则会产生野指针,并输出!!!
#include<iostream>
#include<deque>
using namespace std;
int main(){
deque<int> a;
for(int i=0;i<5;i++)
a.push_back(i);
deque<int>::iterator it;
it=a.begin();
for(;it!=a.end();it++){
cout<<*it<<endl;
a.erase(it);
}
return 0;
}
结果:
1
2
3
4
-572662307
在使用erase()时,应先判断是否为队列中的最后一个元素。