C++11为容器增加了emplace_back成员函数,改函数可以改善push_back的效率:
- #include
- #include
- using namespace std;
-
- class A{
- public:
- A(int i)
- {
- cout<<"A construct, this:"<<this<
- }
- A(const A &a)
- {
- cout<<"A copy construct, this:"<<this<<" ori:"<<&a<
- }
- ~A()
- {
- cout<<"A deconstruct, this:"<<this<
- }
- };
-
- int main() {
- vector v;
- v.push_back(1);
- return 0;
- }
运行程序输出:
A construct, this:0x61fe0f
A copy construct, this:0xd26190 ori:0x61fe0f
A deconstruct, this:0x61fe0f
A deconstruct, this:0xd26190
可以看到push_back会先构造一个对象A,然后再调用copy构造将其加入到容器中
换成emplace_back:
#include &l
-
相关阅读:
JavaScript之运算符相关知识
第一章 ArcGIS Pro python高级脚本教程介绍
【软件测试】正交法设计测试用例的使用
ubuntu22.04安装教程
记一次SpringBoot中Service层未注入排查
用QT实现一个简单的计算机
一文快速学会hadoop完全分布式集群搭建,很详细
价格监测,如何锁定目标链接
苏宁易购获得suning商品详情 API 返回值说明
axios封装get,post请求, 原生xhr ajax封装同步请求
-
原文地址:https://blog.csdn.net/jiemashizhen/article/details/128111115