zoukankan      html  css  js  c++  java
  • STL笔记之【map之添加元素】

    //---------------------------------------------------------
    // 向map中插入元素的方法比较
    //---------------------------------------------------------
    class A
    {
    public:
     A()
     {
      cout << "A()" << endl;
     }

     A(const A& rhs)
     {
      cout << "A(const A&)" << endl;
     }

     ~A()
     {
      cout << "~A()" << endl;
     }
    };

    map<int, A> mapTest;

    //*****************
    // 方法一
    //*****************
    mapTest.insert(map<int, A>::value_type(0, a));
    输出:(3次构造函数)
    A()
    A(const A&)
    A(const A&)
    ~A()
    ~A()
    ~A()

    //*****************
    // 方法二
    //*****************
    mapTest.insert(pair<const int, A>(0, a));
    输出:(3次构造函数)
    A()
    A(const A&)
    A(const A&)
    ~A()
    ~A()
    ~A()

    //*****************
    // 方法三
    //*****************
    mapTest.insert(pair<int, A>(0, a));
    输出:(4次构造函数)
    A()
    A(const A&)
    A(const A&)
    A(const A&)
    ~A()
    ~A()
    ~A()
    ~A()

    //*****************
    // 方法四
    //*****************
    mapTest[0] = a;
    输出:(4次构造函数,实际上还调用了一次operator=)
    A()
    A()
    A(const A&)
    A(const A&)
    ~A()
    ~A()
    ~A()
    ~A()

    //*****************
    // 方法五
    //*****************
    mapTest.insert(make_pair(0, a));
    输出:(5次构造函数)
    A()
    A(const A&)
    A(const A&)
    A(const A&)
    A(const A&)
    ~A()
    ~A()
    ~A()
    ~A()
    ~A()

    //---------------------------------------------------------
    // 总结
    //---------------------------------------------------------
    很显然,方法一、二是最优的,成本最少。
            方法五是最差的,成本最高。

  • 相关阅读:
    AudioStreamer电话打进时崩溃
    ios中NSLog输出格式大全
    IOS Framework制作(一)
    UIButton上的文字添加阴影
    Tim Cook向员工发邮件,祝贺大家实现创纪录的季度
    ios 通讯录“写”操作大全
    AVAudioSession的Category
    IOS播放优酷视频
    iOS 设备的网页调试工具Firebug
    iOS中retain和copy的区别
  • 原文地址:https://www.cnblogs.com/Hisin/p/3152936.html
Copyright © 2011-2022 走看看