I want to free the memory from my set. I tried how to free memory from a set post, but it didn't work for me.
This is my code:
#include <iostream>
#include <set>
using namespace std;
struct Deleter
{
void operator () (int *ptr)
{
delete ptr;
}
};
int main()
{
set<int> myset;
myset.insert(100);
myset.insert(1);
myset.insert(12);
myset.insert(988);
set<int>::iterator it;
for (it = myset.begin() ; it!=myset.end() ; it++)
{
Deleter(it);
cout<<"size of set: "<<myset.size()<<endl;
}
return 0;
}
The output is:
size of set: 4
size of set: 4
size of set: 4
size of set: 4
How does this code free the memory although the size of set is still 4?
What does it delete by Deleter(it)
?
If I use myset.clear()
at the end, would it free all memory from set?
Thanks.