I have a code as follows:
我有一个代码如下:
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.get_the_number函数得到一些输入并返回整数n,它在调用后不会被修改。
-
In the
some_computation
function there is the following code在some_computation函数中有以下代码
std::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); }
Question: Since the size of my_array
is known a priori, is it possible to replace the std::vector
with a std::array
? Moreover, in the affirmative case, should I expect a gain in terms of efficiency?
问题:由于my_array的大小是先验已知的,是否可以用std :: array替换std :: vector?而且,在肯定的情况下,我应该期望在效率方面获得收益吗?
I tried to replace the vector declaration with
我试图用。替换矢量声明
std::array<my_struct,n> my_array;
but I get an error: the size of the array must be constant. Is there a way to avoid it?
但我得到一个错误:数组的大小必须是常量。有没有办法避免它?
Thank you very much.
非常感谢你。
2 个解决方案
#1
12
std::array
needs to know the size at compile time, which doesn't apply to your code. So no, you cannot simply replace std::vector
with std::array
here, unless get_the_number()
can return a constexpr
For example.
std :: array需要知道编译时的大小,这不适用于您的代码。所以不,你不能简单地用std :: array替换std :: vector,除非get_the_number()可以返回constexpr例如。
constexpr int get_the_number() { return 42; }
int main()
{
std::array<int, get_the_number()> a;
}
But presumably in your case int get_the_number()
obtains a number determined at runtime.
但是大概在你的情况下int get_the_number()获得在运行时确定的数字。
#2
5
If you want to use the fact that your array length is a run time constant to improve efficiency, what you want to do is to use std::vector::reserve
to reserve the necessary space ahead of time to save any reallocations as the vector grows - this should make it almost as fast as an array
.
如果你想使用你的数组长度是运行时常量以提高效率的事实,你想要做的是使用std :: vector :: reserve提前保留必要的空间以保存任何重新分配为向量增长 - 这应该使它几乎与数组一样快。
my_array.reserve(get_the_number());
some_computations()
Or if array is local to the function, pass in the number as a parameter.
或者,如果数组是函数的本地数组,则将数字作为参数传入。
#1
12
std::array
needs to know the size at compile time, which doesn't apply to your code. So no, you cannot simply replace std::vector
with std::array
here, unless get_the_number()
can return a constexpr
For example.
std :: array需要知道编译时的大小,这不适用于您的代码。所以不,你不能简单地用std :: array替换std :: vector,除非get_the_number()可以返回constexpr例如。
constexpr int get_the_number() { return 42; }
int main()
{
std::array<int, get_the_number()> a;
}
But presumably in your case int get_the_number()
obtains a number determined at runtime.
但是大概在你的情况下int get_the_number()获得在运行时确定的数字。
#2
5
If you want to use the fact that your array length is a run time constant to improve efficiency, what you want to do is to use std::vector::reserve
to reserve the necessary space ahead of time to save any reallocations as the vector grows - this should make it almost as fast as an array
.
如果你想使用你的数组长度是运行时常量以提高效率的事实,你想要做的是使用std :: vector :: reserve提前保留必要的空间以保存任何重新分配为向量增长 - 这应该使它几乎与数组一样快。
my_array.reserve(get_the_number());
some_computations()
Or if array is local to the function, pass in the number as a parameter.
或者,如果数组是函数的本地数组,则将数字作为参数传入。