关于vector的resize()的理解

vector的resize(),reserve()把我搞的晕头转向,老是记不住。现在把自己的一点理解记录在这里。

先看看http://www.cplusplus.com/reference/vector/vector/resize/

std::vector::resize

void resize (size_type n, value_type val = value_type());


Change size
Resizes the container so that it contains n elements.

If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).

If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.

If n is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.

Notice that this function changes the actual content of the container by inserting or erasing elements from it.


resize()的作用是改变vector中元素的数目。

如果n比当前的vector元素数目要小,vector的容量要缩减到resize的第一个参数大小,既n。并移除那些超出n的元素同时销毁他们。

如果n比当前vector元素数目要大,在vector的末尾扩展需要的元素数目,如果第二个参数val指定了,扩展的新元素初始化为val的副本,否则按类型默认初始化。

注意:如果n大于当前的vector的容量(是容量,并非vector的size),将会引起自动内存分配。所以现有的pointer,references,iterators将会失效。

 

百度知道找到的一个关于resize和reserve的生动的例子:点这里

 resize(),设置大小(size); 

reserve(),设置容量(capacity);
size()是分配容器的内存大小,而capacity()只是设置容器容量大小,但并没有真正分配内存。
打个比方:正在建造的一辆公交车,车里面可以设置40个座椅(reserve(40);),这是它的容量,但并不是说它里面就有了40个座椅,只能说明这部车内部空间大小可以放得下40张座椅而已。而车里面安装了40个座椅(resize(40);),这个时候车里面才真正有了40个座椅,这些座椅就可以使用了。


另外这位博友的blog写的也很清楚,请参考!《c++ vector resize & reserve》


好了,就到这里!

  • 33
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
C++中的resize函数用于改变容器的大小。引用\[1\]中的例子展示了如何使用resize函数来改变vector大小resize函数接受一个参数,表示容器的新大小。如果新大小小于当前大小,那么容器会被截断,多余的元素会被删除。如果新大小大于当前大小,那么容器会被扩展,新添加的元素会被初始化为默认值。引用\[2\]中的例子展示了resize函数的使用情况。在这个例子中,vector大小被先设置为5,然后又被扩展到8,最后又被扩展到12。引用\[3\]中解释了resize函数和reserve函数的区别。resize函数会真正分配内存并初始化新添加的元素,而reserve函数只是设置容器的容量大小,并不会真正分配内存。 #### 引用[.reference_title] - *1* [C++中resize函数的用法](https://blog.csdn.net/jiayizhenzhenyijia/article/details/97898577)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C++中的resize()函数](https://blog.csdn.net/dutmathjc/article/details/105701846)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [关于vectorresize()的理解](https://blog.csdn.net/shixin_0125/article/details/105820582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值