使用随机大小初始化std :: array

时间:2018-03-31 19:48:29

标签: c++ c++11 c++14 std gnu

我有两个宏:

#define length(array) sizeof(array)/sizeof(array[0])
#define randRange(x,y) x + ( std::rand() % ( y - x + 1 ) )

我可以这样做:

int data[] = {2, 3, 34, 10, 3, 12, 30 };
const int arr_length = length(data);
std::array<int,arr_length> data_std_array;

我可以使用常量初始化:

const int arr_length = 500;
std::array<int,arr_length> data_std_array;

但我无法使用我的rand函数初始化std :: array:

float a = randRange(2, 200);
const int arr_length = (int)a;
std::array<int,arr_length> data_std_array;

float a = randRange(2, 200);
int counter = 0;
for(int i =0; i < a; i++){
    counter++;
}
const int arr_length = counter;
std::array<int,arr_length> data_std_array;

float a = randRange(2, 200);
const int arr_length = static_cast<int>(a);

这些都不起作用。

我尝试了各种变量,例如 size_t unsigned ints 常量指针我总是收到此错误消息

错误消息我总是收到:float a = randRange(2,200);     const int arr_length = static_cast(a);

/home/martin/Documents/github/on-line-mean-calculation/tests/test.cpp: In member function ‘virtual void algorithmTest_TestAlgorithmRandomArray_Test::TestBody()’:
/home/martin/Documents/github/on-line-mean-calculation/tests/test.cpp:87:20: error: the value of ‘arr_length’ is not usable in a constant expression
     std::array<int,arr_length> data_std_array;
                    ^
/home/martin/Documents/github/on-line-mean-calculation/tests/test.cpp:76:15: note: ‘arr_length’ was not initialized with a constant expression
     const int arr_length = static_cast<int>(a);
               ^
/home/martin/Documents/github/on-line-mean-calculation/tests/test.cpp:87:30: error: the value of ‘arr_length’ is not usable in a constant expression
     std::array<int,arr_length> data_std_array;
                              ^
/home/martin/Documents/github/on-line-mean-calculation/tests/test.cpp:76:15: note: ‘arr_length’ was not initialized with a constant expression
     const int arr_length = static_cast<int>(a);
               ^
/home/martin/Documents/github/on-line-mean-calculation/tests/test.cpp:87:30: note: in template argument for type ‘long unsigned int’ 
     std::array<int,arr_length> data_std_array;

如何使用范围内的随机长度初始化和标准数组?

2 个答案:

答案 0 :(得分:3)

没有;数组的长度必须是编译时常量,std C ++库中没有编译时随机数引擎。

如果您希望运行时确定连续的数据缓冲区,请使用std::vector。这就是它的用途,以及它的作用。

如果您想要非std随机数编译时常量,请选择4. I rolled it using a fair die.

理论上,您可以使用编码代码编译时间的宏,并根据它编写编译时伪随机数生成器。或者你可以使用continuation传递样式和带有绑定的模板化代码来编译一些大量的K程序,并选择在运行时执行哪一个。但所有这些都是非常坦率的坏主意。

哎呀,你可以让你的C ++程序将C ++程序打印到文件中,在程序中包含C ++编译器,链接动态打印的程序的输出,然后运行该代码。

答案 1 :(得分:3)

std::array编译时大小。这意味着,无论你用它做什么,每次运行程序时,它都会创建一个具有相同数量元素的数组。如果这不是您想要的,那么您应该查看std::vector,它可以在运行时更改大小,因此每次运行程序时都会有所不同。< / p>

无论哪种方式,除非必要,否则不应创建宏。有时他们是,但通常(包括在这些情况下),他们造成的问题比他们解决的更多,你应该使用函数。您的lengthrandRange宏都可以更好地实现为函数,而length已经存在为std::size

运行时随机数生成

如果你想要运行时随机数,这是人们在谈论随机性时通常想要的,你应该使用C++ randomness library,而不是rand function inherited from C。但是,它可能很难使用,除非你比大多数程序员想要了解随机数更多知道;我建议使用一些比较容易使用的库,比如the randutils library

编译时随机数生成

如果由于某种原因,你想在编译时生成一个随机数,那么每次运行程序时都会获得相同的数字但是如果你重新编译就得到一个不同的编号,这在C ++中是可能的11及以后。您可以在线找到a C++ Weekly video about this topic以及其他示例。