2

有人可以帮助我了解 std::map 容器是如何实现的吗?我有一个包含原子成员的类,并且不需要调用复制构造函数,因此我使用 c++11 删除运算符来抑制复制构造函数的隐式生成。

MyCalss(const MyClass& a) = delete;

这在我的 Windows 版本中运行良好,但是在 Linux 中,我收到一个错误消息,通知我 std::map 类的 [] 运算符正在尝试调用已删除的函数。

Windows VS2013 和 Linux GCC 4.7.x 的 map 实现之间似乎存在重大差异。这让我做了一个关于如何将对象插入地图的实验。

我写了这个小示例程序:

#include <stdlib.h>
#include <stdio.h>
#include <map>
#include <iostream>
#include <string>

using namespace std;
class TestItem {
public:
TestItem () { 
    _name = "TestItem" + id();
    cout << "Constructing " << _name << endl;
}
TestItem (const TestItem & other) {
   _name = "TestItem " + id();
   cout << "Copying " << other._name << " to new  " << _name <<endl;
}

string id() 
{
   static int id = 0;
   char buf[2];
   sprintf_s(buf, "%d", id++);
   return string(buf);
}
~TestItem(){
   cout << "Destroying " << _name << endl;
}
void doStuff()
{
   // stub
}

string _name;
};

void run()
{
   cout << "making new obj" << endl;
   TestItem a;
   cout << endl << endl;

   map<string, TestItem> TestItemMap;
   cout << "Makeing new obj as part of a map insert" << endl;
   TestItemMap["foo"].doStuff();
   cout << endl << endl;

   cout << "adding a value to the map" << endl;
   TestItemMap["new foo key"] = a;
   cout << endl << endl;

   cout << "looking up a value that has already been inserted" << endl;
   TestItem& b = TestItemMap["foo"];
   cout << endl << endl;
}
int main(int argc, char** argv)
{
   run();
}

在 Windows 中,当我运行这个程序时,我得到以下输出:

making new obj
Constructing TestItem0

Making new obj as part of a map insert
Constructing TestItem1

adding a value to the map
Constructing TestItem2

looking up a value that has already been inserted

Destroying TestItem1
Destroying TestItem0
Destroying TestItem0

这是我在内部写作时希望看到的

 TestItemMap["foo"].doStuff();

我希望地图会创建一个新的 TestItem 实例,然后通过将树节点内部链接到新的 TestItem 将其插入到 RedBlack 树中。

但是,当我在 Linux 中运行相同的代码时,结果却大不相同

making new obj
Constructing TestItem0

Making new obj as part of a map insert
Constructing TestItem1
Copying TestItem1 to new TestItem2
Copying TestItem2 to new TestItem3
Destroying TestItem2
Destroying TestItem1

adding a value to the map
Constructing TestItem4
Copying TestItem4 to new TestItem5
Copying TestItem5 to new TestItem6
Destroying TestItem5
Destroying TestItem4

looking up a value that has already been inserted

Destroying TestItem0
Destroying TestItem3
Destroying TestItem0

这将向我表明 [] 运算符正在创建 TestItem 的新实例,然后调用外部 map.insert() 函数,然后销毁新创建的 TestItem,这仅解释了对复制构造函数的调用之一。gcc中的c++ stdlib真的这么低效吗?

人们使用一些标准技巧来克服这个问题吗?

4

2 回答 2

3

首先,我解决了那个可怕sprintf_s的事情:

string id() 
{
   static int id = 0;
   std::stringstream s;
   s << id++;
   return s.str();
}

并且还更改了您的“查找已插入的值”以实际执行它所说的[编辑:您也是如此:-)]

现在,在 C++03 模式下使用 g++ 4.8.1 进行编译,我得到的结果与您的相似。但是编译-std=c++11,我得到

making new obj
Constructing TestItem0

Making new obj as part of a map insert
Constructing TestItem1

adding a value to the map
Constructing TestItem2

looking up a value that has already been inserted

Destroying TestItem0
Destroying TestItem1
Destroying TestItem0

似乎 MSVC 自动使用 C++11 特性(最有可能是移动语义)来提供很好的性能提升,而您需要明确告诉 g++ 做同样的事情。

于 2013-11-04T10:48:55.883 回答
2

这似乎是 GCC 4.8 修复的错误。

于 2013-11-04T10:43:30.937 回答