std :: vector help !! [英] std::vector help!!

查看:72
本文介绍了std :: vector help !!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储指向对象指针的stl向量数组。

要删除我正在使用的数组:


std :: vector< * FOO> bar;

....

for(vector< * foo> :: iterator itr = bar.begin(); itr!= bar.end();)

{

删除itr;

itr = NULL;

}


我需要在使用bar.clear()之后清除条形数组吗?

如果我不使用bar.clear()可能会产生什么影响。

I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
....
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}

Do i need to clear the bar array after this using bar.clear();
if i do not use bar.clear() what could be the implications.

推荐答案

文章< 11 ********************** @ c74g2000cwc.googlegroups .com>,

" linux_bp" < RI ********** @ gmail.com>写道:
In article <11**********************@c74g2000cwc.googlegroups .com>,
"linux_bp" <ri**********@gmail.com> wrote:
我有一个存储指向对象的指针的stl向量数组。
删除我正在使用的数组:

std ::矢量< *&FOO GT; bar;
...
for(vector< * foo> :: iterator itr = bar.begin(); itr!= bar.end();)
{
删除itr;
itr = NULL;
}

我需要在使用bar.clear()之后清除bar数组吗?


你没必要,但你可能想要。

如果我不使用bar.clear()可能会有什么影响。
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}

Do i need to clear the bar array after this using bar.clear();
You don''t have to, but you might want to.
if i do not use bar.clear() what could be the implications.




假设bar.size()> 0你的

程序中会有一些空指针,取消引用其中任何一个都会导致未定义的行为,并且

使用push_back添加一个指向对象的新指针不会摆脱任何




Assuming bar.size() > 0 you will have a number of null pointers in your
program, dereferencing any of them will cause undefined behavior, and
adding a new pointer to an object with push_back will not get rid of any
of them.




linux_bp写道:

linux_bp wrote:
I有一个存储指向对象指针的stl向量数组。
要删除我使用的数组:

std :: vector< * foo> bar;
...
for(vector< * foo> :: iterator itr = bar.begin(); itr!= bar.end();)
{
删除itr;
itr = NULL;


我需要在使用bar.clear()之后清除条形数组;
如果我不使用bar.clear()可能有什么影响。
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}

Do i need to clear the bar array after this using bar.clear();
if i do not use bar.clear() what could be the implications.




你需要擦除你从栏中删除的指针。

这将有助于解除引用NULL指针。



You need to erase the pointer that you have delete''d from the bar.
This will help from dereferencing NULL pointers.




linux_bp写道:

linux_bp wrote:
我有存储指向对象的指针的stl向量数组。
删除我正在使用的数组:

std :: vector< * foo> bar;
...
for(vector< * foo> :: iterator itr = bar.begin(); itr!= bar.end();)
{
删除itr;
itr = NULL;
}
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}




这不应该是

delete(* itr);



我猜你正在删除向量

元素引用的对象。也许我错了。



Shouldnt this be
delete (*itr);
?
I guess you are deleting the objects being referred to by the vector
elements. Maybe I am wrong.


这篇关于std :: vector help !!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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