试图理解为什么多重映射会像它那样构造/解构
我先给你打印了所有“地址”。我想理解的是为什么它当时要分配。我在构造函数中使用字符串和对象构造了一个多重映射;这个对象的析构函数在被调用时我有一个打印输出。
第一个问题:它是复制对象吗? 第二个问题:为什么我看到的析构函数多于构造函数? 第三个问题:我从来没有得到我们在下面看到的任何构造函数中列出的地址(最后 5 个)
感谢您帮助我理解这个 multimap 中的构造/销毁。
奥利弗帕
std::multimap <string, TestObject> m;
for(int i = 0; i < 5 ;i++){
TestObjectone("test", i);
m.insert(pair<string, TestObject>(("a" + i), one));
cout << "Single Iteration" << i << endl;
}
拉姆构造函数 test0 0x22ff24 析构函数称为 TestObject:test0 0x22ff08
析构函数称为 TestObject:test0 0x22ff18
单次迭代0
析构函数称为 TestObject:test0 0x22ff24
参数构造函数 test1 0x22ff24
析构函数称为 TestObject:test1 0x22ff08
析构函数称为 TestObject:test1 0x22ff18
单次迭代1
析构函数称为 TestObject:test1 0x22ff24
参数构造函数 test2 0x22ff24
析构函数称为 TestObject:test2 0x22ff08
析构函数称为 TestObject:test2 0x22ff18
单次迭代2
析构函数称为 TestObject:test2 0x22ff24
参数构造函数 test3 0x22ff24
析构函数称为 TestObject:test3 0x22ff08
析构函数称为 TestObject:test3 0x22ff18
单次迭代3
析构函数称为 TestObject:test3 0x22ff24
参数构造函数 test4 0x22ff24
析构函数称为 TestObject:test4 0x22ff08
析构函数称为 TestObject:test4 0x22ff18
单次迭代4
析构函数称为 TestObject:test4 0x22ff24
析构函数称为 TestObject:test4 0x482f6c
析构函数称为 TestObject:test3 0x482efc
析构函数称为 TestObject:test0 0x482dd4
析构函数称为 TestObject:test2 0x482e8c
析构函数称为 TestObject:test1 0x482e1c
I have a printout of all the "addresses" for you first. The thing im trying to understand is why is it allocating at the time. I have constructed a multimap with a String and an Object, in the constructor & destructor of this object I have a printout when they are called.
First Question: Is it copying the Object?
Second Question: Why do i see more destructors than constructors?
Third Question: I never get the address listed in any constructor which we see below (last 5)
Thanks for helping me to understand this construction/destruction in multimap .
Oliver
std::multimap <string, TestObject> m;
for(int i = 0; i < 5 ;i++){
TestObjectone("test", i);
m.insert(pair<string, TestObject>(("a" + i), one));
cout << "Single Iteration" << i << endl;
}
Param constructor test0 0x22ff24
Destructor is called TestObject: test0 0x22ff08
Destructor is called TestObject: test0 0x22ff18
Single Iteration0
Destructor is called TestObject: test0 0x22ff24
Param constructor test1 0x22ff24
Destructor is called TestObject: test1 0x22ff08
Destructor is called TestObject: test1 0x22ff18
Single Iteration1
Destructor is called TestObject: test1 0x22ff24
Param constructor test2 0x22ff24
Destructor is called TestObject: test2 0x22ff08
Destructor is called TestObject: test2 0x22ff18
Single Iteration2
Destructor is called TestObject: test2 0x22ff24
Param constructor test3 0x22ff24
Destructor is called TestObject: test3 0x22ff08
Destructor is called TestObject: test3 0x22ff18
Single Iteration3
Destructor is called TestObject: test3 0x22ff24
Param constructor test4 0x22ff24
Destructor is called TestObject: test4 0x22ff08
Destructor is called TestObject: test4 0x22ff18
Single Iteration4
Destructor is called TestObject: test4 0x22ff24
Destructor is called TestObject: test4 0x482f6c
Destructor is called TestObject: test3 0x482efc
Destructor is called TestObject: test0 0x482dd4
Destructor is called TestObject: test2 0x482e8c
Destructor is called TestObject: test1 0x482e1c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个问题:
是的,STL 容器具有值语义,并且您的对象会被复制。
第二个问题:
可能是因为您的复制构造函数没有调试打印。如果您尚未实现自己的自定义 copy-ctor,则将使用默认的 copy-ctr。
第三个问题:
您会看到复制构造对象的析构函数调用(正如 jkrok 在评论中提到的那样)
First question:
yes, STL-container have a value semantic and your objects are copied.
Second Question:
probably because your Copy Constructor has no debug prints. If you have not implemented your own, custom made copy-ctor, then the defaults copy-ctr will be used.
Third Question:
You see the destructor calls of the copy constructed objects (as also jkrok mentions in the comments)