擦除列表中的元素崩溃。 [英] Crash in erasing element of a list.
问题描述
我有一个指针列表。例如
A * a =新A(); // A是一个类
stl :: list< A * list_a;
我在通过new
运算符。
但是当我想要清除列表中的所有元素时。我的程序崩溃。
我使用迭代器删除元素。
A * temp = NULL;
stl :: list< A *> ; :: iterator Iter,
stl :: list< A *> :: iterator IterTemp;
for(Iter = list_a.begin(); Iter!= list_a .end();){
Iter = list_a.begin();
temp = * Iter;
IterTemp = Iter;
Iter ++;
list_a.erase(IterTemp);
删除临时数据;
}
>
在擦除行中,我的程序崩溃,调试器在
擦除功能中显示一些异常。
非常感谢任何帮助。
提前致谢。
Vibhor Mahajan
I have a list of pointers. e.g
A* a = new A(); // A is a class
stl::list<A*list_a;
I am inserting object of class in the after allocating memeory thru new
operator.
But when i want to erase all elements from the list. my progam crashes.
I delete element by using a iterator.
A * temp = NULL;
stl::list<A*>::iterator Iter,
stl::list<A*>::iterator IterTemp;
for( Iter = list_a.begin() ; Iter != list_a.end() ; ){
Iter = list_a.begin();
temp = *Iter;
IterTemp = Iter;
Iter++;
list_a.erase(IterTemp);
delete temp;
}
At erase line my program crashes and debugger shows some exception in
erase function.
Any help is greatly appreciated.
Thanks in advance.
Vibhor Mahajan
推荐答案
ma************@gmail.com 写道:
ma************@gmail.com wrote:
我有一个指针列表。例如
A * a =新A(); // A是一个类
stl :: list< A * list_a;
我在通过new
运算符。
但是当我想要清除列表中的所有元素时。我的程序崩溃。
我使用迭代器删除元素。
A * temp = NULL;
stl :: list< ; A *> :: iterator Iter,
stl :: list< A *> :: iterator IterTemp;
I have a list of pointers. e.g
A* a = new A(); // A is a class
stl::list<A*list_a;
I am inserting object of class in the after allocating memeory thru new
operator.
But when i want to erase all elements from the list. my progam crashes.
I delete element by using a iterator.
A * temp = NULL;
stl::list<A*>::iterator Iter,
stl::list<A*>::iterator IterTemp;
for(Iter = list_a.begin(); Iter!= list_a.end();){
Iter = list_a.begin();
temp = * Iter;
IterTemp = Iter;
Iter ++;
list_a.erase(IterTemp);
删除临时;
}
for( Iter = list_a.begin() ; Iter != list_a.end() ; ){
Iter = list_a.begin();
temp = *Iter;
IterTemp = Iter;
Iter++;
list_a.erase(IterTemp);
delete temp;
}
擦除后Iter无效。
而不是Iter ++; list_a.erase()执行此操作:
Iter = list_a.erase(Iter);
Iter is invalid after the erase.
instead of Iter++; list_a.erase() do this:
Iter = list_a.erase(Iter);
>
在擦除行中,我的程序崩溃,调试器在
擦除功能中显示一些异常。
非常感谢任何帮助。
提前致谢。
Vibhor Mahajan
>
At erase line my program crashes and debugger shows some exception in
erase function.
Any help is greatly appreciated.
Thanks in advance.
Vibhor Mahajan
Noah Roberts写道:
Noah Roberts wrote:
ma **** ********@gmail.com 写道:
>我有一个指针列表。例如
A * a = new A(); // A是一个类
stl :: list< A * list_a;
我在通过新的
运算符分配memeory后插入类的对象。
但是当我想要清除列表中的所有元素时。我的程序崩溃。
我使用迭代器删除元素。
A * temp = NULL;
stl :: list< A *> :: iterator Iter,
stl :: list< A *> :: iterator IterTemp;
>I have a list of pointers. e.g
A* a = new A(); // A is a class
stl::list<A*list_a;
I am inserting object of class in the after allocating memeory thru new
operator.
But when i want to erase all elements from the list. my progam crashes.
I delete element by using a iterator.
A * temp = NULL;
stl::list<A*>::iterator Iter,
stl::list<A*>::iterator IterTemp;
> for(Iter = list_a.begin(); Iter!= list_a.end();){
Iter = list_a.begin();
temp = * Iter;
IterTemp = Iter;
Iter ++;
list_a.erase(IterTemp);
删除temp;
}
>for( Iter = list_a.begin() ; Iter != list_a.end() ; ){
Iter = list_a.begin();
temp = *Iter;
IterTemp = Iter;
Iter++;
list_a.erase(IterTemp);
delete temp;
}
擦除后Iter无效。
而不是Iter ++; list_a.erase()执行此操作:
Iter = list_a.erase(Iter);
Iter is invalid after the erase.
instead of Iter++; list_a.erase() do this:
Iter = list_a.erase(Iter);
>擦除行我的程序崩溃和调试器在
擦除功能中显示一些异常。
非常感谢任何帮助。
提前致谢。
Vibhor Mahajan
>At erase line my program crashes and debugger shows some exception in
erase function.
Any help is greatly appreciated.
Thanks in advance.
Vibhor Mahajan
真的,当你想做那种事情的时候,*最好的方式去
恕我直言:
std :: list< A *> :: iterator it;
for(it = list_a.begin(); it!= list_a.end() ; ++ it)
{
删除* it;
}
list_a.clear();
它会更快更可读。
Pierre
Really, when you want to do that kind of things, the *best* way to go
IMHO is :
std::list<A*>::iterator it;
for(it = list_a.begin() ; it != list_a.end() ; ++it)
{
delete *it;
}
list_a.clear();
It will be much faster and much more readable.
Pierre
ma ************ @ gmail.com 写道:
我有一个指针列表。例如
A * a =新A(); // A是一个类
stl :: list< A * list_a;
我在通过new
运算符。
但是当我想要清除列表中的所有元素时。我的程序崩溃。
我使用迭代器删除元素。
A * temp = NULL;
stl :: list< ; A *> :: iterator Iter,
stl :: list< A *> :: iterator IterTemp;
for(Iter = list_a.begin(); Iter!= list_a.end();){
Iter = list_a.begin();
temp = * Iter;
IterTemp = Iter;
Iter ++;
list_a.erase(IterTemp);
删除临时;
}
在擦除行中,我的程序崩溃,调试器在
擦除功能中显示一些异常。
I have a list of pointers. e.g
A* a = new A(); // A is a class
stl::list<A*list_a;
I am inserting object of class in the after allocating memeory thru new
operator.
But when i want to erase all elements from the list. my progam crashes.
I delete element by using a iterator.
A * temp = NULL;
stl::list<A*>::iterator Iter,
stl::list<A*>::iterator IterTemp;
for( Iter = list_a.begin() ; Iter != list_a.end() ; ){
Iter = list_a.begin();
temp = *Iter;
IterTemp = Iter;
Iter++;
list_a.erase(IterTemp);
delete temp;
}
At erase line my program crashes and debugger shows some exception in
erase function.
所有,它应该是std :: list,而不是stl :: list。
其次,按如下方式重写你的删除循环:
for (iter = list_a.begin(); iter!= list_a.end;)
{
delete(* iter);
iter = list_a.erase(iter);
}
First of all, it should be std::list, not stl::list.
Second, rewrite your delete loop as follows:
for (iter = list_a.begin(); iter != list_a.end ; )
{
delete (*iter);
iter = list_a.erase(iter);
}
这篇关于擦除列表中的元素崩溃。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!