English 中文(简体)
STL 涉及物体的集装箱[复制]
原标题:STL containers with reference to objects [duplicate]
This question already has answers here:
Closed 4 years ago.

The community reviewed whether to reopen this question last year and left it closed:

原先的近因没有得到解决

我知道STL集装箱复制了这些物体。 因此,我要说的是:

list<SampleClass> l;

每次我做事

SampleClass t(...);
l.push_back(t);

a 定本。 如果SampleClass是大型的,那将是非常昂贵的。

但是,如果我宣布灯为参考资料的集装箱,

list<SampleClass&> l;

l.push_back(t);

它是否避免复制物体?

最佳回答

令人痛心的是,它赢得了汇编(至少是分机)。 但是,将集装箱内物体的点子储存起来的替代办法将完全加以汇编。

这将给你留下在您的代码上外加的yn子——你们不得不用新东西把货物插入你的集装箱。

std::list<class_type*> l;
l.push_back(new class_type);

然而,虽然现在赢得的物体被涂 co,但在清单被拆除时,这些物体也自动清理。 聪明的点子会为你们解决该问题,但代价是更多的同化噪音。 既然你可以 st住:auto_ptr 装在标准集装箱内,因为可以 t,你必须使用其微薄的超重加权表,共用点子。

std::list<boost::shared_ptr<class_type> > l;
l.push_back(boost::shared_ptr<class_type>(new class_type));

Shared pointed do incur some extra overhead, but it is minimal.

问题回答

如果你知道你做了些什么,你可以使用<代码>std:参比_wrapper<>/code:

#include <functional>
#include <vector>

int main()
{
  std::vector<std::reference_wrapper<int>> iv;

  int a = 12;
  iv.push_back(a);  // or std::ref(a)

  // now iv = { 12 };

  a = 13;

  // now iv = { 13 };
}

Note of course that any of this will come crashing down on you if any of the referred-to variables go out of scope while you re still holding references to them.

标准图书馆集装箱要求其类型可以复制;因为参考资料不是,你可以将其储存在集装箱内,从头开始。 只要你仔细研究物体寿命,你就可以储存点。 诱杀装置有点容器帮助,或者有智能点。 但请注意,auto_ptr is not copiable (as the standard definitions it for this purpose), sojointd_ptr and unique_ptr are Yousnow. 两者在C++11中都是标准,前者通过C++03的提升而得到支持。

您希望有一个点子:

list<SampleClass*> l;

既然你有聪明的点子,你就可以利用它来进行记忆管理,从他们那里提取原始点子在STL集装箱中使用。 这样,你就把所有权保留在集装箱之外。

在此,我拥有一个独特的树木吸收器,但利用STL堆储存原材料。

void TreeTraversal(unique_ptr<BinaryTreeNode>& root) {
    stack<BinaryTreeNode *> _stack;

    BinaryTreeNode *p = root.get();
    _stack.push(p);

    while(!_stack.empty()) {
        p = _stack.top();
        _stack.pop();
        ...
        _stack.push(p->left);
        _stack.push(p->right);
    }
}

int main() {
    unique_ptr<BinaryTreeNode> root = unique_ptr<BinaryTreeNode>(new BinaryTreeNode(...));
    TreeTraversal(root);
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签