CGor
CGor

Reputation: 67

How to clear a map with map in it?

map<int, map<int, vector<string> > > test;
// some statement to assign values to test
test.clear();

Is above enough to release all memory used by test or I have to iterate the map to release the inner map and vector one by one?

Upvotes: 3

Views: 377

Answers (2)

Teebrin
Teebrin

Reputation: 117

It is enough to free all memory allocated by all sub containers. It is true for all STL containers.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726669

Yes, your code is sufficient to clear the inner maps and the vectors they contain. Destructors of the containers will take care of clearing out the resources held in keys and values of the maps. Of course the emptied map itself will not be deallocated until it gets destroyed, either by going out of scope or by virtue of its owner object being deleted.

Upvotes: 7

Related Questions