C++string的shrink_to_fit
时间: 2024-01-06 15:04:18 浏览: 121
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 ]
阅读全文