码迷,mamicode.com
首页 > 其他好文 > 详细

ch02

时间:2017-03-10 23:27:18      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:运行   ast   img   png   pair   space   log   ios   vector   

 

 

 

 

2.4 emplace_back减少内存拷贝

 

 

 

#include <vector>
#include <map>
#include <string>
#include <iostream>

using namespace std;

struct Complicated
{
    int year;
    double country;
    std::string name;

    Complicated(int a, double b, std::string c) :year(a), country(b), name(c)
    {
        cout << "is conrtusted" << endl;
    }
    
    Complicated(const Complicated & other)
        : year(other.year)
        , country(other.country)
        , name(std::move(other.name))
    {
        cout << "is moved" << endl;
    }
};

int main()
{
    std::map<int, Complicated> m;
    int aInt = 4;
    double aDouble = 5.0;
    std::string aString = "C++";
    cout << "--insert--" << endl;

    m.insert(std::make_pair(4, Complicated(aInt, aDouble, aString)));

    cout << "--emplace--" << endl;
    m.emplace(4, Complicated(aInt, aDouble, aString));

    cout << "--emplce_back--" << endl;
    vector<Complicated> v;
    v.emplace_back(aInt, aDouble, aString);
    cout << "--push_back--" << endl;
    v.push_back(Complicated(aInt, aDouble, aString));

    system("pause");
}

运行结果:

技术分享

ch02

标签:运行   ast   img   png   pair   space   log   ios   vector   

原文地址:http://www.cnblogs.com/wuya-study/p/6533375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!