English 中文(简体)
C++ st:校长记忆/定位
原标题:C++ std::vector memory/allocation

https://stackoverflow.com/questions/2663170/stdvector- Capacity- subsequently-copying”a Bailey先生先前关于病媒能力的问题:

在目前的C++中,保证在要求保留后不发生重新定位,直到插入时,其规模将超出先前呼吁的储备价值。 在呼吁保留之前,或在呼吁保留时,如果规模是先前呼吁保留的价值与执行能力之间,则允许在作出选择时尽早重新分配。

因此,如果我正确理解,为了保证在能力超过之前不会发生重新分配,我必须两次保留? 请澄清这一点吗?

我将病媒作为记忆套:

std::vector<double> memory;
memory.reserve(size);
memory.insert(memory.end(), matrix.data().begin(), matrix.data().end()); // smaller than size
memory.resize(memory.capacity(), 0);

我需要保证,在上述情况下不会发生重新分配。

谢谢。

p:我也想知道,除了病媒之外,是否有更好的办法管理记忆袋。

最佳回答

我认为,你重新阅读这一说法是错误的。 允许保留地将<条码> 能力设定为超出您保留的数额。 特殊语言是,如果你在达到目前的能力之前重新作出保留,就可以重新定位。

问题回答

它将在需要时重新定位。 你不需要两次保留。 如果你重新做许多插入,并且你非常了解你重新储存的规模,那么使用储备是一个好的想法。 其速度要快得多。

您不需要两次打电话<代码> > 保留<>/代码>。

此外,在所贴出的代码样本中,你只读到 Supp一次。 实际上,您重新编辑的代码对病媒的<代码>size产生影响。 见23.3.6.2.11 (C++0x FCD) on effects of Without resize(size_type sz, const T& c);:

if (sz > size())
    insert(end(), sz-size(), c);
else if (sz < size())
    erase(begin()+sz, end());
else
     ; // do nothing

因此,基本上,当你打电话memory.resize(memory. Capacity()、 0)时,你实际上是在0memory. 能力()——在你打电话到之后的倍。





相关问题
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?