尝试在没有智能指针的情况下使用 STL - 尝试避免临时对象创建

发布于 2024-09-06 07:00:33 字数 607 浏览 1 评论 0原文

我特别喜欢以直接的方式使用 STL 容器的简单性。

我从来没有真正弄清楚如何让 Boost 库在我的开发平台上工作,事实上我认为我什至没有尝试过。我想你可能会说我只是想推迟不可避免的事情,因为 Boost 显然是一个我应该使用的有用的库。

但我的问题本质上与这个主题相同: 如何在不调用复制构造函数的情况下使用类初始化 STL 向量/列表

mylist; 我只想要一个将新实例推入列表并调用默认构造函数的函数,而不是从它的临时堆栈实例中复制它。在另一个主题中提到了移动构造函数。我查阅了它们,坦率地说,这除了让我心中感到恐惧之外没有任何作用。两个&符号!!

如果我只创建一系列 ExpenseClass 对象,它会起作用吗? ExpenseClass *mylist = new ExpenseClass[20]; 这会调用构造函数 20 次吗?

在我看来我应该只使用 boost:ptr_list 。

I particularly like the simplicity of using STL containers in the straightforward way.

I have never really figured out how to get the Boost library working on my dev platforms, in fact I don't think I've even tried. I guess you could say I am just trying to delay the inevitable since Boost is clearly a helpful library that I should be using.

But my question is essentially the same as this topic: How to initialise a STL vector/list with a class without invoking the copy constructor

I have std::list<ExpensiveClass> mylist; and I just want a function that pushes a new instance into the list and calls the default constructor, rather than copying it from a temporary stack instance of it. In the other topic there was mention of move constructors. I looked them up and quite frankly it does nothing but strike fear into my heart. Two ampersands!!

Would it work if I just made an array of ExpensiveClass objects? ExpensiveClass *mylist = new ExpensiveClass[20]; Does this call the constructor 20 times?

Seems to me I should just use boost:ptr_list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

榕城若虚 2024-09-13 07:00:33

将对象插入到容器中会调用该对象的复制构造函数。目前确实没有办法解决这个问题,因此为什么指针容器用于大型对象:复制指针非常便宜。

如果您选择使用智能指针容器,则可以使用 Boost 指针容器之一,也可以使用 shared_ptrs

回答您的问题:

ExpensiveClass *mylist = new ExpensiveClass[20];

ExppressiveClass 的默认构造函数被调用 20 次:数组的每个元素调用一次。

Inserting an object into a container invokes the copy constructor on that object. There's really no way around that (yet), hence why pointer containers are used for large objects: it's dirt cheap to copy a pointer.

If you choose to use a container of smart pointers, you can either use one of the Boost pointer containers or you can use an STL container of shared_ptrs.

To answer your question about:

ExpensiveClass *mylist = new ExpensiveClass[20];

The default constructor for ExpensiveClass is called 20 times: once for each element of the array.

奢欲 2024-09-13 07:00:33

我只想要一个将新实例推入列表并调用默认构造函数的函数

既然您提到了“两个与号”,我将为您提供 C++0x 解决方案:

mylist.emplace_back();

如果您想使用不同的构造函数,只需提供参数:

mylist.emplace_back(whatever, arguments, your, constructor, takes);

此成员函数就地构造对象,无需复制(或移动)。

I just want a function that pushes a new instance into the list and calls the default constructor

Since you mentioned "two ampersands", I am going to give you the C++0x solution:

mylist.emplace_back();

If you want to use a different constructor, simply provide arguments:

mylist.emplace_back(whatever, arguments, your, constructor, takes);

This member function constructs the object in-place, without copying (or moving).

快乐很简单 2024-09-13 07:00:33

根据对 C++0x 功能的实现支持,请查看 emplace_back 和类似的成员函数 - 它们正是您所要求的,即直接在容器内“就地”构造一个对象,没有任何复制。

Depending on implementation support for C++0x features, have a look at emplace_back and similar member functions - they do precisely what you ask for, i.e. construct an object directly inside the container, "in-place", without any copying.

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