stl矢量储备 [英] stl vector reserve
本文介绍了stl矢量储备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图像这样预先分配整数的向量
I was trying to pre-allocate a vector of integer like this
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> 0(我选中)。在for循环之后,tmp_color的大小似乎为零。如果代码有问题,
where node_obj[node].c_max is > 0 (I checked). size of tmp_color appears to be zero anyways, after the for loop. If there something wrong with the code?
感谢
推荐答案
你想在循环中进行赋值,我写的建议如下:
If you want to make assignments in the loop as you wrote I suggest the following way:
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;
这篇关于stl矢量储备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文