std::vector::assign - 重新分配数据? [英] std::vector::assign - reallocates the data?

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

问题描述

我正在使用 STL 库,我的目标是尽量减少数据重新分配的情况.我在徘徊,是吗

I am working with STL library and my goal is to minimize the data reallocation cases. I was wndering, does

std::vector::assign(size_type n, const value_type& val)

std::vector::assign(size_type n, const value_type& val)

如果大小未更改或实际上只是分配新值(例如,使用 operator=),则重新分配数据?

reallocated the data if the size is not changed or does is actually just assign the new values (for example, using operator=) ?

http://www.cplusplus.com/ 上的 STL 文档说明如下(C++98):

The STL documentation at http://www.cplusplus.com/ sais the following (C++98):

在填充版本(2)中,新内容是n个元素,每个元素都初始化为val的一个副本.如果发生重新分配,则使用内部分配器分配所需的存储空间.

In the fill version (2), the new contents are n elements, each initialized to a copy of val. If a reallocation happens,the storage needed is allocated using the internal allocator.

在调用之前保存在容器中的任何元素都将被销毁并替换为新构造的元素(不进行元素分配).当且仅当新向量大小超过当前向量容量时,这会导致自动重新分配已分配的存储空间.

Any elements held in the container before the call are destroyed and replaced by newly constructed elements (no assignments of elements take place). This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.

不进行元素分配"这句话让人有点困惑.

The phrase "no assignments of elements take place" make it all a little confusing.

例如,我想要一个类向量(例如,OpenCV 的 cv::Vec3i).这是否意味着

So for example, I want to have a vector of classes (for example, cv::Vec3i of OpenCV). Does this mean, that

  1. cv::Vec3i 的析构函数或构造函数会被调用吗?
  2. 是否会直接复制 Vec3i 内存并填充向量?
  3. 如果我的类在运行时使用 new 分配内存会发生什么操作员?这个内存不能用普通内存来解释复制.这是否意味着,assign() 不应该用于此类对象?
  1. the destructor or constructor of cv::Vec3i will be called?
  2. a direct copy of Vec3i memory will be made and fills the vector?
  3. what happens, if my class allocates memory at run time with new operator? This memory cannot be accounted for by plain memory copying. Does it mean, that assign() should not be used for such objects?

在这种情况下使用赋值的全部目的是将向量中的所有值设置为 0(如果我有 std::vector v).它会做很多次.std::vector 本身的大小不会改变.

the whole purpose of using assign in this case is to set all values in the vector to 0 (in case I have std::vector< cv::Vec3i > v). It will be done many-many times. The size of std::vector itself will not be changed.

我想做(以更短的方式)如下:

what i want to do (in a shorter way) is the following:

 for(int i=0; i<v.size(); i++)  
   for(int j=0; j<3; j++)
     v[i][j] = 0;

现在我对 C++98

推荐答案

assign 的语义以一种非常直接的方式定义了标准:

The semantics of assign are defined the standard in a quite straightforward way:

void assign(size_type n, const T& t);

void assign(size_type n, const T& t);

效果:

erase(begin(), end());
insert(begin(), n, t);

这意味着首先将调用元素的析构函数.t 的副本是在元素生命周期结束后留下的原始存储中制作的.

This means that first the destructors of the elements will be called. The copies of t are made in what is now a raw storage left after the lifetime of the elements ended.

要求 value_typeMoveAssignable(用于当 erase 没有擦除到容器的末尾并且需要将元素移向开头时).

The requirements are that value_type is MoveAssignable (for when erase doesn't erase to the end of the container and needs to move elements towards the beginning).

insert 重载要求 value_typeCopyInsertableCopyAssignable.

insert overload used here requires that value_type is CopyInsertable and CopyAssignable.

无论如何,vector 不知道你的类是如何管理自己的资源的.那是你要照顾的.请参阅三法则.

In any case, vector is oblivious to how your class is managing its own resources. That's on you to take care of. See The Rule of Three.

这篇关于std::vector::assign - 重新分配数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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