[英]How to declare an array without size known at compile-time?
I am trying to implement random_function()
, which outputs the amount of random numbers within a range (user input), and count_function()
to count for the value that the user wants to look up in the outputted random numbers.我正在尝试实现
random_function()
,它输出范围内(用户输入)内的随机数的数量,以及count_function()
来计算用户想要在输出的随机数中查找的值。 I made the random numbers to be saved in an array called randomlist[]
so that it can be used to count for the value from the outputted random numbers.我将随机数保存在一个名为
randomlist[]
的数组中,以便它可以用于计算输出随机数的值。 However, I cannot declare an array without its fixed size in the header file and define its size in the count_function.cpp.但是,我不能在 header 文件中声明一个没有固定大小的数组,并在 count_function.cpp 中定义它的大小。 So I cannot use
randomlist[]
in the count_function()
without redefining it (which is meaningless...).所以我不能在
count_function()
中使用randomlist[]
而不重新定义它(这是没有意义的......)。
Is there a way to declare array without defining its size, in the header file?有没有办法在 header 文件中声明数组而不定义其大小?
#pragma once
using namespace std;
class Rand
{
public:
// Rand();
int range,min,max;
char key;
void Random_function();
void Count_function();
// randomlist[]; something like this, which that is not possible in c++
};
#include <iostream>
#include "random_function.hpp"
using namespace std;
void Rand::Random_function()
{
beginning:
cout << "Enter amount of numbers to generate: ";
cin >> range;
if (range<=0 || cin.fail())
{
cout <<"Error! Please enter valid number and try again! "<<endl;
goto beginning;
}
else
{
reenter:
cout << "Enter minimum boundary: ";
cin >> min;
cout << "Enter maximum boundary: ";
cin >> max;
cout << "\n";
srand((unsigned)time(NULL));
if(max<min || cin.fail())
{
cout << "\nError! Please enter the valid value and try again! " << endl;
goto reenter;
}
else
{
int randomlist[range]; //I defined the size of the randomlist here but I want to use this outside of the random_fucntion() as well.
for(int i=0 ; i < range; i++)
{
randomlist[i] = min + (rand() % static_cast<int>(max - min + 1));
cout <<i+1<<". "<< randomlist[i] <<endl;
}
}
cout <<"\n"<< "Total random numbers generated: " << range<< endl;
cout <<"Do you want to continue? y/n"<<endl;
cin >> key;
if(key=='y')
{
Count_function();
cout <<"Do you want to restart? y/n"<<endl;
cin >> key;
if(key=='y')
{
goto beginning;
}
else
{
exit(0);
}
}
else
{
cout <<"Do you want to restart? y/n"<<endl;
cin >> key;
if(key=='y')
{
goto beginning;
}
else
{
exit(0);
}
}
}
}
void Rand::Count_function()
{
int n,count=0;
reenter2:
cout<<"Enter the value to count for: ";
cin>>n;
if(cin.fail())
{
cout<<"Please enter valid value to count for"<<endl;
goto reenter2;
}
else
{
for(int i=0 ; i <range; i++)
{
if(randomlist[i]==n)
{
count++;
}
}
}
cout <<"The number of '"<<n<<"'s in the given list is: "<< count <<endl;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>
#include "random_function.hpp"
using namespace std;
int main()
{
Rand call;
call.Random_function();
}
When you want to use an array, but the size cannot be known at compile-time, the generally accepted approach in C++ is to use a std::vector
.当您想使用数组,但在编译时无法知道大小时,C++ 中普遍接受的方法是使用
std::vector
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.