If I have a container like a std::set
of pointers to dynamic objects then how can I free its elements?
int main()
{
// new scope
{
int x = 10;
std::set<int*> spi;
spi.insert(new int(1));// elem is a dynamic object init from 1
spi.insert(new int[3]()); // elem is a dynamic array of 3 default-init integers
spi.insert(&x); // elem is address of stack memory object
}
}
So how I can free elements with dynamic memory efficiently?
- I know I can use a set of shared_ptrs or unique_ptr but for practice sake I want to know how to.