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()

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

  • 相关阅读:
    Echarts
    Django入门--模型系统(二):常用查询及表关系的实现
    Django入门--模型系统(一):模型基础
    Django入门--自定义过滤器与标签
    Django入门--模板标签、继承与引用
    Django入门--模板变量、过滤器及静态文件
    类的继承
    https://docs.python.org/3.6/library/index.html
    9*9
    赋值,浅复制,深复制
  • 原文地址:https://www.cnblogs.com/Hisin/p/3152936.html
Copyright © 2011-2022 走看看