相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams Well after the clear call the data in the map myMap have been destroyed, so the question is moot actually as the "inner" maps doesn't exist anymore. Some programmer dude Jan 18, 2016 at 6:59 You can easily add debug output to the destructor of contained objects to find out. That said, consider using two ints as key in the map, like map<pair<int,int>, int> . My gut feeling is that this is closer to what you want to express actually. Ulrich Eckhardt Jan 18, 2016 at 7:16 It won't clear myInsideMap , but it will destroy all the maps it contains. myInsideMap is local to the constructor, so it's destroyed when the constructor exits. molbdnilo Jan 18, 2016 at 8:06

No. myMap.clear() will destroy its own copy of myInsideMap . myMap will cease to exist when the constructor() scope is exited.

Standard library containers own their elements. When they are cleared or cease to exist, their elements are destroyed too.

Quoting a note from the standard section 23.2.1 that talks about General container requirements:

the destructor is applied to every element of a; all the memory is deallocated.

I expect myMap.clear() will clear myInsideMap too

Nope. myMap clears the contents of mapMap only which includes a std::pair object internally which in turn has a copy of myInsideMap.

myInsideMap was allocated on the stack and hence it is deallocated when its local scope is over when constructor() exits. Again, this is not where the myInsideMap (copy) inside the myMap is destructed.

is this cleared correctly?? recursively?

Yes, myMap is cleared here but the recursive destruction happens as follows - the std::pair destructor is called internally for which the key and values are destructed. Since, the copy of myInsideMap was the value within this pair structure, that content is also destroyed. So the destructive "recursion" doesn't touch the local or the actual myInsideMap in constructor().

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .

 
推荐文章