English 中文(简体)
矢量储备
原标题:stl vector reserve

我正试图预先释放像这种愤怒的病媒。

vector<int> tmp_color; tmp_color.reserve(node_obj[node].c_max);
    for(int tmp_color_idx = 0; tmp_color_idx < node_obj[node].c_max; tmp_color_idx++)
         tmp_color[tmp_color_idx] = tmp_color_idx;

Node_obj[node]。 c_max is > 0(Iecked)。 t形体的大小似乎为零。 如果对法典有某种错误?

Thanks

最佳回答

如果你想像你写的那样在 lo中做任务,我建议采取下列方式:

vector<int> tmp_color(node_obj[node].c_max);
for(int tmp_color_idx = 0; tmp_color_idx < node_obj[node].c_max; tmp_color_idx++)
     tmp_color[tmp_color_idx] = tmp_color_idx;
问题回答

实际没有在<条码>上添加内容;<条码>的>条码”; 他们只有保留空间。 你们要么需要在座右边逐个添加内容,使用:

tmp_color.push_back(tmp_color_idx);

或改变您对<条码>的利用。

你们很幸运的是,它确实发生故障。

tmp_color[tmp_color_idx] = tmp_color_idx;

在上述路线上,你正在接触病媒的束缚。

reserve() doesn t increase the size of the vector, resize() should be used. To me, the method used by Elalfer is even better to pre-allocate the size.





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

热门标签