vector shrink_to_fit
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int>vec;
for(int i = 0 ;i < 100 ; ++i)
vec.push_back(i);
cout << vec.size() << endl; //100
cout << vec.capacity() << endl; //128
vec.erase(vec.begin()+10,vec.end()); //改變了size,但是並未改變capccity
cout << vec.size() << endl; //10
cout << vec.capacity() << endl; //128
vector<int>(vec).swap(vec);
cout << vec.size() << endl; //10
cout << vec.capacity() << endl; //10
vec.clear(); //clear並未真正釋放空間!!!
cout << vec.size() << endl; //0
cout << vec.capacity() << endl; //10
vector<int> (vec).swap(vec); //這才真正釋放了空間!!
cout << vec.size() << endl; //0
cout << vec.capacity() << endl; //0
return 0;
}
PS:C++11中已經實現了shink_to_fit函式。實現上述功能。
相關文章
- vector::shrink_to_fit()
- STL vector中的shrink_to_fit方法(32)
- C++ shrink_to_fit()的實現C++
- C++ sort vector<vector<int> > or vector<MyClass> 容器的排序C++排序
- vector
- vector::clear(),容器vector的clear函式詳解。函式
- Support Vector MachinesMac
- vector——C++C++
- c++ vectorC++
- Vector擴容
- Paimon Deletion VectorAI
- C++中vector*和vector有什麼區別C++
- Vector 原始碼分析原始碼
- Vector原始碼分析原始碼
- vector does not name a type
- C++ Vector fundamentalC++
- C++ STL -- vectorC++
- C++(std::vector)C++
- C++學習之路(vector::clear和vector::erase的區別)C++
- C++的vector容器C++
- 初探STL容器之Vector
- C++之vector容器C++
- Android中的VectorAndroid
- java arrayList vector 區別Java
- STL:vector用法總結
- vector 二維陣列陣列
- Rust中如何排序Vector?Rust排序
- C++:vector assignC++
- 【java】【集合】List、ListIterator、VectorJava
- 進階篇_vector容器
- vector和iterator及collection
- vector 學習初步(1)
- c++ vector容器、字串C++字串
- Vector + ClickHouse 收集日誌
- vector的使用注意點
- python-Vector向量Python
- C++簡單vectorC++
- vector訪問與賦值賦值