shrink_to_fit_vector :: shrink_to_fit()函数以及C ++ STL中的示例

shrink_to_fit

C ++ vector :: shrink_to_fit()函数 (C++ vector::shrink_to_fit() function)

vector::shrink_to_fit() is a library function of "vector" header, which is used to reduce capacity to fit size. Refer to example to understand in details.

vector :: shrink_to_fit()“ vector”标头的库函数,用于减少容量以适应大小。 请参阅示例以详细了解。

This may lead to reallocation, but elements don't alter.

这可能会导致重新分配,但元素不会改变。

Note: To use vector, include <vector> header.

注意:要使用向量,请包含<vector>标头。

Syntax of vector::shrink_to_fit() function

vector :: shrink_to_fit()函数的语法

    vector::shrink_to_fit();

Parameter(s): none – It accepts nothing.

参数: 无 –不接受任何内容。

Return value: void – It returns nothing.

返回值: void –不返回任何内容。

Example:

例:

    Input:
    //capacity is initialized to be 100
    vector<int> arr(50); 
    arr.capacity() =50
    
    Resize:
    //doesn't change capacity though
    arr.resize(10); 
    arr.capacity() =50
    
    shrink_to_fit:
    //changes capacity as per resize, 
    //thus this practically reduced the capacity
    arr.shrink_to_fit();
    arr.capacity() =10

C ++程序演示vector :: shrink_to_fit()函数的示例 (C++ program to demonstrate example of vector::shrink_to_fit() function)

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> arr(50); //capacity is initialized to be 100
    cout << "...capacity of the vector: " << arr.capacity() << "...\n";

    arr.resize(10); //doesn't change capacity though
    cout << "...After resizing...\n";
    cout << "capacity of the vector: " << arr.capacity() << "\n";

    arr.shrink_to_fit(); //changes capacity as per resized vector
    cout << "...After using shrink_to_fit...\n";
    cout << "capacity of the vector: " << arr.capacity() << "\n";

    return 0;
}

Output

输出量

...capacity of the vector: 50... 
...After resizing... 
capacity of the vector: 50 
...After using shrink_to_fit...
capacity of the vector: 10 

Reference: C++ vector::shrink_to_fit()

参考: C ++ vector :: shrink_to_fit()

翻译自: https://www.includehelp.com/stl/vector-shrink_to_fit-function-with-example.aspx

shrink_to_fit

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++的string类提供了shrink_to_fit()函数,可以使string的容量(capacity)随着size的缩小而缩小。\[2\]当我们使用resize()函数来改变string的大小时,如果size缩小后,字符超出的部分会被裁剪掉,但是capacity不会减小。而shrink_to_fit()函数可以在size缩小后,将capacity也相应地缩小,以节省内存空间。\[2\]这个函数C++11被引入,可以通过调用string对象的shrink_to_fit()方法来使用。 #### 引用[.reference_title] - *1* [C++ string清空并释放内存空间的两种方法(shrink_to_fit()、swap())](https://blog.csdn.net/qq_41687938/article/details/117731243)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [c++ string的详细用法(15)reserve()与resize()与shrink_to_fit()](https://blog.csdn.net/qq_40630246/article/details/103655230)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【C++】如何释放vector的内存空间及std::vector::shrink_to_fit用法简介](https://blog.csdn.net/weixin_43753894/article/details/126970068)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值