调整大小并清除矢量 [英] resize and clear of vector

查看:77
本文介绍了调整大小并清除矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用向量来保留一定数量的内存,并为新的数据集重用

,以避免重新分配内存。 />
因此呼叫类似于

vector< my_datav; ///临时存储。

v.reserve(300); /// at max我有300个数据。


现在,my_data没有默认的ctor(因为它没有默认值

状态。


一次又一次地使用向量v,

我在做什么,

v.resize(0) ;

////然后使用它。我可以做v.clear()更好。

现在的问题是,

1)确定零容量,即释放内存? (然后我不能用它,

因为迭代矢量会重新分配内存)

2)调整大小比现有尺寸减小容量

(然后调整大小(0)也会做同样的事情)

3)做一个尺寸小于现有尺寸的调整大小需要

default(或任何其他ctor)?


谢谢

解决方案

toton写道:


1)清除零容量,即释放内存?



编号std :: vector永远不会释放内存(除非它被销毁,

当然)。


2)调整尺寸小于现有尺寸减少容量



编号

如果你想释放std :: vector所占用的内存,那就有一个已知的技巧:


{

std :: vector< my_datatmp;

v.swap(tmp);

}

//这里''v''将有0大小并且没有分配内存


请注意,大括号在那里非常相关。


< blockquote> totonaécrit:




我使用向量来保留一定数量的内存,并重用

它用于新的数据集,以避免重新分配内存。

因此调用类似于

vector< my_datav; ///临时存储。 <无线电通信/>
v.reserve(300); /// at max我有300个数据。


现在,my_data没有默认的ctor(因为它没有默认值

状态。


一次又一次地使用向量v,

我在做什么,

v.resize(0) ;

////然后使用它。我可以做v.clear()更好。

现在的问题是,

1)确定零容量,即释放内存? (然后我不能使用它,

因为迭代矢量将重新分配内存)



标准不要求因此AFAIK现有的实现

不要。


2)调整尺寸小于现有尺寸减小容量

(然后调整大小(0)也会做同样的事情)



标准不需要这样的AFAIK现有实现

不要。


3)做一个尺寸小于现有尺寸的调整大小需要

默认(或任何其他ctor) )?



没理由它。


释放矢量记忆的常用技巧是:

vector< myClassmy_vector(100000);


//通过交换空向量清除

std :: vector< myClass>()。swap(my_vector) );

Michael


totonaécrit:




我使用向量来保留一定数量的内存,并为新的数据集重用

,以避免重新分配内存。

so这个电话就像

vector< my_datav; ///临时存储。

v.reserve(300); /// at max我有300个数据。


现在,my_data没有默认的ctor(因为它没有默认值

州)。

[snip]

3)做一个尺寸小于现有尺寸的尺寸要求

默认(或任何其他ctor) )?



我不明白你对默认构造函数的担忧。


std :: vector不需要默认构造函数。您可以将

对象用作构造作为第二个参数。


vector< my_datav(10,my_data(42);


调整大小也是如此:

v.resize(72,my_data(36);


Michael


Hi,
I am using a vector to reserve certain amount of memory, and reuse
it for new set of data, to avoid reallocation of memory.
so the call is something like
vector<my_datav;///the temporary storage.
v.reserve(300); ///at max I have 300 data.

now, my_data doesn''t have a default ctor (as it doesn''t have a default
state).

to use vector v again and again,
I am doing ,
v.resize(0);
////and then use it. I can do v.clear() for better.
Now the questions are,
1) do clear zero''s capacity , i.e frees memory? (Then I can''t use it,
as it iteration the vector will reallocate memory)
2) do resize to a size smaller than existing size reduces capacity
(Then resize(0) will also do the same thing )
3) do a resize with a size smaller than existing size requires
default (or any other ctor ) ?

thanks

解决方案

toton wrote:

1) do clear zero''s capacity , i.e frees memory?

No. std::vector never frees memory (except when it''s destroyed,
of course).

2) do resize to a size smaller than existing size reduces capacity

No.

If you want to free the memory taken by a std::vector, there''s
a known trick for that:

{
std::vector<my_datatmp;
v.swap(tmp);
}
// Here ''v'' will have 0 size and will have no memory allocated

Note that the curly brackets are very relevant there.


toton a écrit :

Hi,
I am using a vector to reserve certain amount of memory, and reuse
it for new set of data, to avoid reallocation of memory.
so the call is something like
vector<my_datav;///the temporary storage.
v.reserve(300); ///at max I have 300 data.

now, my_data doesn''t have a default ctor (as it doesn''t have a default
state).

to use vector v again and again,
I am doing ,
v.resize(0);
////and then use it. I can do v.clear() for better.
Now the questions are,
1) do clear zero''s capacity , i.e frees memory? (Then I can''t use it,
as it iteration the vector will reallocate memory)

It is not required by the standard thus AFAIK existing implementations
don''t.

2) do resize to a size smaller than existing size reduces capacity
(Then resize(0) will also do the same thing )

It is not required by the standard thus AFAIK existing implementations
don''t.

3) do a resize with a size smaller than existing size requires
default (or any other ctor ) ?

No reason it should.

The usual trick to release memory of a vector is:
vector<myClassmy_vector(100000);

//clear by swapping with empty vector
std::vector<myClass>().swap(my_vector);
Michael


toton a écrit :

Hi,
I am using a vector to reserve certain amount of memory, and reuse
it for new set of data, to avoid reallocation of memory.
so the call is something like
vector<my_datav;///the temporary storage.
v.reserve(300); ///at max I have 300 data.

now, my_data doesn''t have a default ctor (as it doesn''t have a default
state).
[snip]
3) do a resize with a size smaller than existing size requires
default (or any other ctor ) ?

I don''t understand your concern about default constructor.

std::vector doesn''t require a default constructor. You can pass the
object to use for construction as second parameter.

vector<my_datav(10,my_data(42);

The same is true of resize:
v.resize(72,my_data(36);

Michael


这篇关于调整大小并清除矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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