简体   繁体   English

当通过命令行 arguments 从配置文件中给出数组的大小时,我们如何使用 std::array?

[英]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的允许值集在编译时已知(例如110100 ),您可以为这些值中的每一个生成代码(但这可能导致代码膨胀),然后使用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.

相关问题 std::array 的维度可以从命令行 arguments 读取吗? - Can the dimension of std::array be read from command line arguments? 如何使用数组接受命令行参数? - How to use an array to take command line arguments? 如何在C ++ 11中使用命令行参数的大小创建动态数组? - How to create a dynamic array in C++11 with size from command line arguments? 我们不能从initializer_list创建std :: array,但是可以使用带有可变参数的辅助函数来创建它吗? - We cannot create an std::array from an initializer_list, but can we create it with a helper function with variadic arguments? 如何将值附加到命令行参数数组? - How to append a value to the array of command line arguments? C ++默认数组大小使用命令行参数覆盖 - c++ default array size override with command line arguments 如何创建一个void数组,其大小将由命令行中的用户输入指定? - How can I create an array of void whose size will be specified by the user input from command line? 我们可以在std :: array中使用常规的指针算法吗? - Can we use conventional pointer arithmetic with std::array? C ++如何将命令行参数转换为数组? - How C++ turns command line arguments into an array? 如何将可变内容常量用作 std::array 的大小? - How to turn a variable content constant for use it as the size of std::array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM