试图理解为什么多重映射会像它那样构造/解构

发布于 2025-01-04 19:38:33 字数 1425 浏览 1 评论 0原文

我先给你打印了所有“地址”。我想理解的是为什么它当时要分配。我在构造函数中使用字符串和对象构造了一个多重映射;这个对象的析构函数在被调用时我有一个打印输出。

第一个问题:它是复制对象吗? 第二个问题:为什么我看到的析构函数多于构造函数? 第三个问题:我从来没有得到我们在下面看到的任何构造函数中列出的地址(最后 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦一生花开无言 2025-01-11 19:38:33

第一个问题:
是的,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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文