[C++] emplace_back and push_back

在向容器,如std::vector中添加元素时,常常使用push_back(var),这个时候,往往元素要经过拷贝(copy)或者移动(move)才能放进容器中。

而emplace_back()方法则能够就地构造元素(参数列表与元素某个构造函数的参数列表相同,该方法直接将构造的对象放到容器中,不用经过拷贝或者移动),效率更高。

References
https://stackoverflow.com/questions/4303513/push-back-vs-emplace-back
https://blog.csdn.net/xiaolewennofollow/article/details/52559364

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.