【问题标题】:STL Map Value ConstructorsSTL 映射值构造函数
【发布时间】:2010-09-22 16:27:48
【问题描述】:

我有一个 X 类,我想将它放入 std::map 类型的 STL 映射中。 STL 映射需要将 X 存储在内存中的某个位置,因此我正在寻找一种有效的(运行时和内存)方法来创建 X 并将其存储在映射中。

我注意到以下代码,其中 x 是 X 类型的对象,stlMap 是 std::map 类型的映射:

stlMap["test"] = x;

导致以下被调用:

  1. X 默认构造函数
  2. X 复制构造函数
  3. X 复制构造函数
  4. X 析构函数
  5. X 析构函数
  6. X 赋值构造函数
  7. X 析构函数

为什么要创建这么多 X 对象?

这是对时间和内存的低效利用吗?

有没有更好的方法将对象放入地图中?也许将映射更改为字符串到 x* 的映射?

【问题讨论】:

  • 您是否尝试过开启优化。这将删除很多这些。

标签: c++ stl


【解决方案1】:

试试stlMap.insert( map<string, X>::value_type("test", x) ):

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

using namespace std;

class X
{
public:
X() { cout << "X default constructor" << endl; }
~X() { cout << "X destructor" << endl; }
X( const X& other ) { cout << "X copy constructor" << endl; }
X& operator=( const X& other ) { cout << "X copy-assignment operator" << endl; }
int x;
};


int main()
{
X x;
map< string, X > stlMap;

cout << "INSERT BEGIN" << endl;
stlMap.insert( map< string, X >::value_type( "test", x ) );
cout << "INSERT END" << endl;
stlMap.clear();
cout << "ASSIGN BEGIN" << endl;
stlMap["test"] = x;
cout << "ASSIGN END" << endl;

return 0;
}

在我的 g++ 上,它把事情缩减到:

  1. X 拷贝构造函数
  2. X 拷贝构造函数
  3. X 析构函数

编辑:根据 ArunSaha 的建议,更新了测试。 insert() 输出不变,而赋值序列如下所示:

  1. X 默认构造函数
  2. X 复制构造函数
  3. X 复制构造函数
  4. X 析构函数
  5. X 析构函数
  6. X 复制赋值运算符

【讨论】:

  • +1:我在考虑同样的实验……你能在复制赋值运算符中添加cout 并重新运行实验吗?
  • @ArunSaha:很好,我知道我忘记了什么 :) 更新了。
【解决方案2】:

STL 容器具有复制语义,因此您观察到的是典型的。

您可以改用指针,但可以通过使用智能指针避免相关的内存管理痛苦(以少量开销为代价)。

【讨论】:

    【解决方案3】:

    以此为参考:

    #include <iostream>
    #include <map>
    
    class X
    {
        public:
         X()                    { std::cout << "Default Construct\n";}
        ~X()                    { std::cout << "Destroy\n";}
         X(X const&)            { std::cout << "Copy Construct\n";}
         X& operator=(X const&) { std::cout << "Assignment\n";}
    };
    
    
    int main()
    {
        std::map<int,X>     store;
        X                   x;
        X                   y;
    
        std::cout << "Inserting x\n";
        store[1]    = x;
        std::cout << "Finished Insert\n";
        std::cout << "Inserting y\n";
        store[1]    = y;
        std::cout << "Finished Insert\n";
    }
    

    运行我们得到以下输出:

    Default Construct                    Building X
    Default Construct                    Building Y
    Inserting x                          ---- Start of an insert
    Default Construct                    -------- Work to insert an item that is not in the map
    Copy Construct                        
    Copy Construct                       
    Destroy                              
    Destroy                             -------- Finished work to insert a new item
    Assignment                          Assign x into the internal object
    Finished Insert                     ---- Done
    Inserting y                         ---- Start of an insert
    Assignment                          Assign y onto the internal object.
    Finished Insert                     ---- Done
    Destroy                             Destroy y
    Destroy                             Destroy x
    Destroy                             Destroy map containing one X
    

    【讨论】:

      【解决方案4】:

      STL 在复制和分配的基础上工作。因此,预计会有一些复制和分配。关于为什么这么多的问题,找到它的一种方法是在X代码中的适当位置放置断点,然后在调试器中运行程序。

      目前,std::map 正在为X 进行内存管理。如果您切换到使用X*,那么您必须自己进行内存管理。我发现前者适用于大多数情况。

      【讨论】:

        【解决方案5】:

        启用优化时,构造和销毁的顺序是否不同?许多编译器在优化时可以忽略此类语句中涉及的临时对象。我猜输出在优化后会涉及更少的对象。

        C++0x 使用 move 构造函数 大大改善了这种情况。如果您的 X 类有一个移动构造函数(看起来像 X x(X&amp;&amp; m) { ... },那么您可以将您的语句更改为 stlMap["test"] = std::move(x);。该语句本质上等同于构造一个 X,然后将其移动到地图的所有权。查找如果你不了解的话,一些关于 C++0x 的文章会移动语义,这是有用的东西。

        【讨论】:

          猜你喜欢
          • 2013-04-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-29
          • 1970-01-01
          • 2013-01-14
          • 2011-01-21
          • 1970-01-01
          相关资源
          最近更新 更多