Is the vector inside the card clear safe?

Just a quick question if you say:

using namespace std;

map< int, vector< string > > map1;

or maybe even:

map< int, map< int, vector< string > > > map2;

and you get the right idea:

map< int, map< int, map< int, vector< string > > > > map3;

if i just:

map1.clear();
map2.clear();
map3.clear();

Is it safe that it empties everything on the map and its embedded maps, vectors, lists, etc.?

Note: I know if you use pointers, you need to manually go through and delete, or if the map is out of scope, it should be okay too. I am particularly interested in this case when it is in scope and on the stack.

+4
source share
2 answers

Yes, the card will destroy all components.

If its components are STL containers, their destructors will clear the containers.

STL, std::map

+2

, . STL .

, , , :

std::vector<MyClass*> vec;
vec.push_back(new MyClass());
vec.clear(); // you get a memory leak here because you did not delete the object you allocated.
+2

Source: https://habr.com/ru/post/1536183/


All Articles