Which way is better/faster in c++11 to clear a container (e.g. queue):
void clean()
{
std::queue<int> empty_q;
std::swap(q_to_clear, empty_q);
}
or using operator=(Q &&) (faster than swap ?)
void clean ()
{
q_to_clear = std::queue<int>{};
}
Or is it essentially the same ?
It probably makes almost no difference at all, but the move-assignment requires that the temporary source queue needs to build a new, empty state after having been moved-from, which you can avoid with the swapping. And you can write the swapping as follows:
C++11
-ness raised to extreme:Works on other
std::
containers too.and compact memory syntax is as cool as: