擦除向量中的成员时崩溃 [英] Crash while erasing a member in a vector

查看:77
本文介绍了擦除向量中的成员时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我在擦除

向量中的最后一个成员时遇到了一个starnge问题。我正在使用VC ++ .NET 2002编译器。我有矢量

CComPtr< ..> (这里不相关),然后我迭代向量。如果

它是迭代器,那么我使用

vecObjects.erase(it)从向量中删除元素。它工作正常,直到最后一个元素。当

删除最后一个元素时,它会抛出异常而失败。但是相同的

vecObject.clear()可以解决任何问题。那里有人可以,

请帮我识别这个问题并解决这个问题


提前致谢。

EK

Hello,

I am facing a starnge problem while erasing the last member in a
vector. I am using VC++ .NET 2002 complier. I have vector of
CComPtr<..> (irrelevant here), and then I iterate over the vector. If
it is the iterator, then I remove the element from the vector using
vecObjects.erase(it). It works fine till the last element. While
removing the last element it throws exception and fails. But the same
vecObject.clear() works with out any problem. Can somebody there,
please help me to indentify this problem and solution to this

Thanks in advance.
EK

推荐答案

在文章< 11 ********************** @ z14g2000cwz.googlegroups .com>,

< ee **** @ gmail.com>写道:
In article <11**********************@z14g2000cwz.googlegroups .com>,
<ee****@gmail.com> wrote:
我在擦除
向量中的最后一个成员时遇到了一个starnge问题。我正在使用VC ++ .NET 2002编译器。我有矢量CCORPtr< ..> (这里不相关),然后我迭代向量。如果它是迭代器,那么我使用
vecObjects.erase(it)从向量中删除元素。它工作正常,直到最后一个元素。当
删除最后一个元素时,它会抛出异常并失败。但是相同的
vecObject.clear()可以解决任何问题。那里有人可以,
请帮我识别这个问题并解决这个问题
I am facing a starnge problem while erasing the last member in a
vector. I am using VC++ .NET 2002 complier. I have vector of
CComPtr<..> (irrelevant here), and then I iterate over the vector. If
it is the iterator, then I remove the element from the vector using
vecObjects.erase(it). It works fine till the last element. While
removing the last element it throws exception and fails. But the same
vecObject.clear() works with out any problem. Can somebody there,
please help me to indentify this problem and solution to this




你最有可能不在矢量的末尾并删除

无效的迭代器。请发布一个可以编译的代码片段和

你会得到更好的回复。

-

Mark Ping
em****@soda.CSUA.Berkeley.EDU

是的,向量中的''last''位置是超出最后一个元素的位置。


如果您正在浏览您的vectory数组,请检查地址是否为已到达最后一个位置的地址。请参阅下面的

代码示例。


vector< int> :: iterator iter = 0;

for(iter = vecList .begin(); iter!= vecList.end(); iter ++)

{

delete * iter;

}

Yes, the ''last'' position in a vector is one beyond the last element.

If you are progressing through your vectory array, check if the address
of the iterator has reached the address of the last position. See the
code example below.

vector<int>::iterator iter = 0;
for( iter = vecList.begin(); iter != vecList.end(); iter++ )
{
delete *iter;
}




" Jordan" <乔*********** @ gmail.com>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com ...

"Jordan" <jo***********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
是的,向量中的''last''位置超出了最后一个元素。

如果你正在通过你的vectory数组,检查地址
迭代器已到达最后一个位置的地址。请参阅下面的代码示例。

vector< int> :: iterator iter = 0;
for(iter = vecList.begin(); iter!= vecList.end( ); iter ++)
{
删除* iter;
}
Yes, the ''last'' position in a vector is one beyond the last element.

If you are progressing through your vectory array, check if the address
of the iterator has reached the address of the last position. See the
code example below.

vector<int>::iterator iter = 0;
for( iter = vecList.begin(); iter != vecList.end(); iter++ )
{
delete *iter;
}




这是如何从a删除动态分配的对象vector,但是你声明的
迭代器引用的是一个没有指针的向量,它只是int'的b $ b。你不想删除一个int,或者通过new创建的任何不是
的东西。 (我知道,这只是一个例子,但这是一个不好的例子。

:-))


OP询问是否使用擦除()从向量中删除项目。这与

的不同之处在于删除了存储在向量中的指向

指针的动态分配数据。没有看到他的代码,我们就不能说

_for_sure_问题是什么。我的猜测是clear()会正常工作,

但这只是猜测。只有当他还想删除

动态分配的对象时,他才会想要一个像你所展示的那样的循环。

-Howard



That''s how to delete dynamically allocated objects from a vector, but the
iterator you''ve declared refers to a vector that does not have pointers, it
has just int''s. You don''t want to delete an int, or anything that was not
created via new. (I know, it''s just an example, but it''s a bad example.
:-))

The OP asked about using erase() to remove items from a vector. That''s
different from deleting the dynamically allocated data pointed to by
pointers stored in the vector. And without seeing his code, we can''t say
_for_sure_ what the problem is. My "guess" is that clear() will work fine,
but it''s just a guess at this point. Only if he also wants to delete
dynamically allocated objects will he first want to have a loop like the one
you''ve shown.

-Howard


这篇关于擦除向量中的成员时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆