[英]How can we use std::array when the size of the array is given from the config file via command line arguments?
I am trying to use std::array
whose size is given by a config file via command line argument in C++.我正在尝试使用大小由配置文件通过 C++ 中的命令行参数给出的
std::array
。
The config looks like this配置看起来像这样
100 // array size
... // other application parameters
Even in a more simpler setting where the array size is expected to be determined via command line argument, ie ./main 100
, this wouldn't work since the array size is not determined at compile time.即使在期望通过命令行参数确定数组大小的更简单的设置中,即
./main 100
,这也不起作用,因为数组大小不是在编译时确定的。
#include <array>
int main(int argc, char** argv) {
if (argc != 2)
return 1;
int n = stoi(argv[1]);
std::array<int, n> arr;
... // do my job
}
Are there any hacks to do the same thing using std::array
, not dynamic STL containers?是否有任何黑客可以使用
std::array
做同样的事情,而不是动态 STL 容器?
You can't.你不能。
You need a dynamic container, if your dimensions are given at runtime.如果您的尺寸在运行时给出,您需要一个动态容器。 That's kind of the definition of dynamic!
这就是动态的定义!
If you are forbidden to use dynamic allocation, you will have to have a large std::array
, with potentially many unused elements and a maximum supported upper bound for the figure in your config file.如果您被禁止使用动态分配,您将必须拥有一个大的
std::array
,其中可能包含许多未使用的元素以及配置文件中图形的最大支持上限。
If the set of permissible values for n
is known at compile-time (say, 1
, 10
or 100
), you can generate code for each of these values (this can lead to a code bloat, however) and then use if
s to select the branch at run-time.如果
n
的允许值集在编译时已知(例如1
、 10
或100
),您可以为这些值中的每一个生成代码(但这可能导致代码膨胀),然后使用if
s select 运行时的分支。 With C++17 fold expressions this idea can be implemented in the following way:使用 C++17 折叠表达式,这个想法可以通过以下方式实现:
template<std::size_t n>
void foo_impl(/* some parameters */) {
std::array<int, n> arr;
// ...
}
template<std::size_t... ns, typename... Args>
void foo(std::size_t n, Args&&... args) {
assert(((n == ns) || ...));
((n == ns && (foo_impl<ns>(std::forward<Args>(args)...), true)), ...);
}
const std::size_t n = /* run-time value */;
foo<1, 10, 100>(n, /* some parameters */);
Here n == ns && expr
is a well-known "hack" to evaluate expr
only if the condition n == ns
evaluates to true
.这里
n == ns && expr
是一个众所周知的“hack”,仅当条件n == ns
评估为true
时才评估expr
。
std::array
requires you to know the size at compile time. std::array
要求您在编译时知道大小。 The only way to do what you say would be to have your program compile another program with the right size value and execute it.做你所说的唯一方法是让你的程序编译另一个具有正确大小值的程序并执行它。
Best option here is probably to use a dynamic size container like std::vector
.这里最好的选择可能是使用像
std::vector
这样的动态大小容器。
Are there any hacks to do the same thing using std::array, not dynamic STL containers?
是否有任何黑客可以使用 std::array 做同样的事情,而不是动态 STL 容器?
No, there are no such hacks.不,没有这样的黑客。 It is only possible to use
std::array
with a compile time constant size.只能使用具有编译时间常数大小的
std::array
。
If you want dynamic size, then you need to use a dynamic array (or some other dynamic data structure).如果您想要动态大小,那么您需要使用动态数组(或其他一些动态数据结构)。
It is not possible to define an std::array
length on runtime.无法在运行时定义
std::array
长度。
You could used std::vector
instead.您可以改用
std::vector
。
It behaves (kind of) like an array, just it allocated the block for the memory within the heap.它的行为(有点)像一个数组,只是它为堆内的 memory 分配了块。
Your could could then be like this:你可能会是这样的:
int n = stoi(argv[1]);
std::vector<int> my_data{};
my_data.resize(n); //Allocates n elements
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.