我有一段如下的代码:
int n;
int get_the_number();
void some_computations();
int main()
{
n = get_the_number();
some_computations()
return(0);
}
The
get_the_number
function get some input and returns the integern
, which after its call will not be modified.In the
some_computation
function there is the following codestd::vector<my_struct> my_array; for(int i=0; i<n; i++) { my_struct struct_temp; // fill struct_temp; my_array.push_back(struct_temp); }
问题: 由于my_array
的大小是预先知道的,是否可能用std::array
代替std::vector
?
此外,在肯定的情况下,我应该期望效率方面的提升吗?
我尝试用以下代码替换向量声明:
std::array<my_struct,n> my_array;
但我遇到了一个错误:数组的大小必须是常量。是否有方法可以避免这个问题发生? 非常感谢。