Delete vector contents and free up memory in C++
This post will discuss how to delete the vector’s contents and free up the memory allocated by the vector to store objects in C++.
1. Using vector::clear
function
We can use the vector::clear
function to remove all elements from the vector. It works by calling a destructor on each vector object, but the underlying storage is not released. So, we’re left with a vector of size 0 but some finite capacity.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream> #include <vector> int main() { std::vector<int> v = { 1, 2, 3, 4, 5 }; v.clear(); std::cout << "The vector size is " << v.size() << ", and its " << "capacity is " << v.capacity(); return 0; } |
Output:
The vector size is 0, and its capacity is 5
Starting with C++11, we can call the vector::shrink_to_fit
function after clear()
, which reduces the vector’s capacity to fir the size. It works by “requesting” a reallocation on the vector.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> #include <vector> int main() { std::vector<int> v = { 1, 2, 3, 4, 5 }; v.clear(); v.shrink_to_fit(); std::cout << "The vector size is " << v.size() << ", and its " << "capacity is " << v.capacity(); return 0; } |
Output:
The vector size is 0, and its capacity is 0
2. Using vector::erase
function
Another solution is to call the vector::erase
function on the vector, as shown below. This also suffers from the same problem as the clear()
function, i.e., reallocation of memory is not guaranteed to happen on the vector.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> #include <vector> int main() { std::vector<int> v = { 1, 2, 3, 4, 5 }; v.erase(v.begin(), v.end()); v.shrink_to_fit(); std::cout << "The vector size is " << v.size() << ", and its " << "capacity is " << v.capacity(); return 0; } |
Output:
The vector size is 0, and its capacity is 0
3. Using vector::resize
function
We can also use the vector::resize
function for resizing the vector. Notice that this function does not destroy the vector objects, and all references to objects remain valid.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> #include <vector> int main() { std::vector<int> v = { 1, 2, 3, 4, 5 }; v.resize(0); v.shrink_to_fit(); std::cout << "The vector size is " << v.size() << ", and its " << "capacity is " << v.capacity(); return 0; } |
Output:
The vector size is 0, and its capacity is 0
4. Using vector::swap
function
All the above solutions fail to release the memory allocated to the vector object without calling the vector::shrink_to_fit
function. The shrink_to_fit()
shrinks the capacity to fit the size, but we can’t entirely rely on it. This is because it makes a non-binding request, and the vector implementation is free to ignore it completely.
There is another workaround to free the memory taken by the vector object. The idea is to swap the vector with an empty vector (with no memory allocated). This will de-allocate the memory taken by the vector and is always guaranteed to work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> #include <vector> int main() { std::vector<int> v = { 1, 2, 3, 4, 5 }; std::vector<int>().swap(v); std::cout << "The vector size is " << v.size() << ", and its " << "capacity is " << v.capacity(); return 0; } |
Output:
The vector size is 0, and its capacity is 0
That’s all about deleting vector contents and free up memory in C++.
<iframe data-google-container-id="4" data-load-complete="true" frameborder="0" height="1" id="google_ads_iframe_/22771334302/TechieDelight_S2S_Leaderboard_ROS_BTF_0" marginheight="0" marginwidth="0" name="google_ads_iframe_/22771334302/TechieDelight_S2S_Leaderboard_ROS_BTF_0" scrolling="no" title="3rd party ad content" width="1"></iframe>Thanks for reading.
Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and help us grow. Happy coding :) Resources Online IDE Company Techie Delight © 2023 All Rights Reserved.
#include <iostream> #include <vector> class TestClass { public: TestClass() { } ~TestClass() { std::cout << "\r\nTestClass destroy \r\n"; } }; int main() { TestClass a;TestClass b; TestClass c; TestClass d; TestClass e; std::vector<TestClass> v = { a,b,c,d,e }; std::vector<TestClass>().swap(v); std::cout << "The vector size is " << v.size() << ", and its " << "capacity is " << v.capacity(); return 0; } output: TestClass destroy TestClass destroy TestClass destroy TestClass destroy TestClass destroy TestClass destroy TestClass destroy TestClass destroy TestClass destroy TestClass destroy The vector size is 0, and its capacity is 0 TestClass destroy TestClass destroy TestClass destroy TestClass destroy TestClass destroy
标签:std,function,capacity,up,free,vector,memory,include From: https://www.cnblogs.com/ioriwellings/p/17497746.html