简体   繁体   English

如何声明一个在编译时不知道大小的数组?

[英]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 文件中声明数组而不定义其大小?

Here is the source code:这是源代码:

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++
};
This is randomfunction.cpp:这是随机函数.cpp:
#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;
}
main:主要的:
#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.

相关问题 如何使 object 获取并存储任意但编译时已知大小的数组? - How to make an object take and store an Array of arbitrary, but compile-time known size? 如何为编译时已知的参数的多个值编译函数 - How to compile a function for multiple values of a parameter known in compile-time 如何在常量 header 文件中声明不同大小列表的编译时常量列表? - How to declare a compile-time constant list of different size lists in a constant header file? 指向指针的指针数组,其中值在编译时已知 - Array of pointers to pointers where values are known at compile-time 如何声明std :: array的全局编译时常量? - How can I declare a global compile-time constant for std::array? 如果在编译时大小未知,如何在堆栈上分配数组? - how can I allocate an array on the stack if the size is not known at compile time? 如何使用可选的编译时参数声明模板化函数? - How to declare a templated function with an optional compile-time parameter? 如果已知,则使用编译时常量 - Use compile-time constant if known 标准if / else在编译时已知的条件? - Standard if/else on conditions known at compile-time? 为什么全局变量在编译时不知道? - Why global variables are not known at compile-time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM