English 中文(简体)
如何改变C++的STL集装箱的规模
原标题:How to change size of STL container in C++

I have a piece of performance critical code written with pointers and dynamic memory. I would like to rewrite it with STL containers, but I m a bit concerned with performance. Is there a way to increase the size of a container without initializing the data?

例如,不这样做

ptr = new BYTE[x];

我想做的是这样的事情。

vec.insert(vec.begin(), x, 0);

However this initializes every byte to 0. Isn t there a way to just make the vector grow? I know about reserve() but it just allocates memory, it doesn t change the size of the vector, and doesn t allows me to access it until I have inserted valid data.

谢谢大家。

最佳回答

<代码>vec.resize (newsize )的定义是,其效果与

vec.insert( vec.end(), newsize - vec.size(), T() )

如果newsize > vec.size(......,但汇编者可能难以确定其正在增长,而不是缩小,而且其数量如何。 你可以尝试对两者进行定性分析。

如果你确信违约的初始阶段需要时间,就会与你自己的建筑商明确消除。 (即使含蓄的构造者被罚款,也能够显示意图。)

struct BYTE {
    char v;
    BYTE() {} // no default initialization!
    BYTE( char x ) : v(x) {}
    operator char&() { return v; }
    operator char const&() const { return v; }
    char *operator&() { return &v; } // not sure about operator&...
    char const *operator&() const { return &v; } // probably good in this case
};
问题回答

vector.resize(......) 或许会有帮助? 你可以具体说明你想要达到(规模更大或较小)的面积,也使你想要成为新内容的人感到气愤。

转()

它可能或不能做任何与您的(a)项不同的事情。 价值是设定的。





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

热门标签