恒定大小的向量 [英] Constant-sized vector
本文介绍了恒定大小的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道定义恒定大小矢量的方法吗?
Does someone know the way to define constant-sized vector?
例如,不是定义
std::vector<int>
它将是
std::vector<10, int>
应该完全跨平台。也许是开源类?
推荐答案
std :: vector始终可以动态增长,但是可以通过两种方式分配初始大小:
The std::vector can always grow dynamically, but there are two ways you can allocate an initial size:
这会分配初始大小并用零填充元素:
This allocates initial size and fills the elements with zeroes:
std::vector<int> v(10);
v.size(); //returns 10
这将分配初始大小,但不会在数组中填充零:
This allocates an initial size but does not populate the array with zeroes:
std::vector<int> v;
v.reserve(10);
v.size(); //returns 0
这篇关于恒定大小的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文