C++ STL container ::clear ::swap
问题 What's the fastest way to "clear" a large STL container? In my application, I need to deal with large size std::map , e.g., 10000 elements. I have tested the following 3 methods to clear a std::map . Create a new container every time I need it. Calling map::clear() method. Calling map::swap() method. It seems that ::swap() gives the best result. Can anyone explain why this is the case, please? Is it safe to say that using map::swap() method is the proper way to "clear" a std::map? Is it the