当前位置:首页 > 芯闻号 > 充电吧
[导读]emplace操作是C++11新特性,新引入的的三个成员emlace_front、empace 和 emplace_back,这些操作构造而不是拷贝元素到容器中,这些操作分别对应push_front、

emplace操作是C++11新特性,新引入的的三个成员emlace_front、empace 和 emplace_back,这些操作构造而不是拷贝元素到容器中,这些操作分别对应push_front、insert 和push_back,允许我们将元素放在容器头部、一个指定的位置和容器尾部。

两者的区别 

当调用insert时,我们将元素类型的对象传递给insert,元素的对象被拷贝到容器中,而当我们使用emplace时,我们将参数传递元素类型的构造函,emplace使用这些参数在容器管理的内存空间中直接构造元素。

一个例子

MyString.h

#ifndef MYSTRING_H
#define MYSTRING_H
#includeclass MyString
{
public:
    MyString(const char *str = NULL);// 普通构造函数
    MyString(const MyString &other);// 拷贝构造函数
    ~MyString(void);// 析构函数
    MyString & operator = (const MyString &other);// 赋值函数
private:
    char *m_data;// 用于保存字符串
};

#endif // MYSTRING_H

MyString.cpp

#include "MyString.h"
#include#include//普通构造函数
MyString::MyString(const char *str)
{
    if (str == NULL)
    {
        m_data = new char[1];
        *m_data = '';
    }
    else
    {
        int length = strlen(str);
        m_data = new char[length + 1];
        strcpy(m_data, str);
    }
    std::cout<<"construct:"<<m_data<<std::endl;
}


// String的析构函数
MyString::~MyString(void)
{
    std::cout<<"deconstruct:"<<m_data<<std::endl;
    delete[] m_data;
}


//拷贝构造函数
MyString::MyString(const MyString &other)
{
    int length = strlen(other.m_data);
    m_data = new char[length + 1];
    strcpy(m_data, other.m_data);
    std::cout<<"copy construct:"<<m_data<<std::endl;
}


//赋值函数
MyString & MyString::operator = (const MyString &other)
{
    std::cout<<"copy assignment"<<std::endl;
    if (this == &other)
        return *this;
    if (m_data)
        delete[] m_data;
    int length = strlen(other.m_data);
    m_data = new char[length + 1];
    strcpy(m_data, other.m_data);
    return *this;
}

main.cpp

#include#include "MyString.h"

int main()
{
    {
        std::cout<<"++++++++++++++++++++++++++++++++++"<<std::endl;
        std::vectorvStr;
        // 预先分配,否则整个vector在容量不够的情况下重新分配内存
        vStr.reserve(100);    
        vStr.push_back(MyString("can ge ge blog"));
    }
    {
        
        std::cout<<"++++++++++++++++++++++++++++++++++"<<std::endl;
        std::vectorvStr;
        // 预先分配,否则整个vector在容量不够的情况下重新分配内存
        vStr.reserve(100);
        vStr.emplace_back("hello world");
    }
    
    system("pause");
    return 0;
}

输出结果


从结果可以看出,vStr.push_back(MyString("can ge ge blog")) 这个语句首先执行了构造函数,构造一个临时对象,接着执行拷贝构造将临时对象复制到vector,最后销毁临时对象和vector中的元素。而emplace_back只调用一次构造函数和一次析构函数。两相对比,效率上的提高不言而喻

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

c++程序员面试过程中基本上都会被问到c++11新特性吧,你是怎么回答的呢? 本文基本上涵盖了c++11的所有新特性,并有详细代码介绍其用法,对关键知识点做了深入分析,对重要的知识点我单独写了相关文章并附上了相关链接,我...

关键字: c++11

很多人谈到c++,说它特别难,可能有一部分就是因为c++的内存管理吧,不像java那样有虚拟机动态的管理内存,在程序运行过程中可能就会出现内存泄漏,然而这种问题其实都可以通过c++11引入的智能指针来解决,相反我还认为这...

关键字: c++11

c++11关于并发引入了好多好东西,这里按照如下顺序介绍: std::thread相关 std::mutex相关 std::lock相关 std::atomic相关 std::call_once相关 volatile相关...

关键字: 线程 c++11

以前,在lambda表达式没有进入标准的时候,对容器的遍历等涉及到使用函数指针的情况,一般人会懒得使用std::for_each,或std::transform,也许只是一个短短的几句话,却要单独写个

关键字: c++ c++11

C++11终于知道要在语言中加入匿名函数了。匿名函数在很多时候可以为编码提供便利,这在下文会提到。很多语言中的匿名函数,如C++,都是用Lambda表达式实现的。Lambda表达式又称为lambda函

关键字: c++11 lambda函数

const引用在C++语言中,引用是作为一种高效,安全的传递数据的方式而存在的。除了一般的引用类型,还可以声明const引用。我们有以下一个Image类。class Image { public:  

关键字: c++11 const引用 右值引用

让我们从std::make_unique和std::make_shared之间的比较开始讲起吧。std::make_shared是C++11的一部分,可惜的是,std::make_unique不是,它

关键字: c++ c++11

为什么需要别名下面的说明只是一个例子,实际的使用场景一定不止这些。假设有一个二维图形计算的程序,定义了一个point结构体。struct point{   int x;   int y;};在有些系统

关键字: c++11 类型别名

熟悉C++98/03的对于for循环就再了解不过了,如果我们要遍历一个数组,那么在C++98/03中的实现方式:int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1

关键字: c++ c++11

问题还是老问题考虑下面的Rect类:struct Rect {     Rect(int l, int t, int r, int b)         :left{l}, top{t}       

关键字: c++ emplace
关闭
关闭