Is it possible to allow a user to enter an array size with a keyboard?

后端 未结 5 1715
萌比男神i
萌比男神i 2021-01-13 10:21

Is it possible to allow the user to enter the size of an array with a keyboard?

I know that arrays can\'t change size. The only solution I could think of is this:

相关标签:
5条回答
  • 2021-01-13 11:08

    Variable Length Arrays are not approved by C++ standard. C++ Standard mandates that the size of an array must be an compile time constant.
    You can use VLA's through compiler extension provided in gcc but note the your code is not portable if you do so.

    Standard approved way of doing this is to use std::vector.

    0 讨论(0)
  • 2021-01-13 11:12

    No, arrays cannot be variably sized in C++. The code you have there will not compile, because the value of a constant has to be evaluated at compile-time.

    You might try the std::vector class instead and use the resize method, or pass the size through the constructor. Although, depending on what you need, maybe you can just let it grow naturally by using push_back.

    A lower-level alternative is to use dynamically allocated arrays:

    int userSize;
    cin >> userSize;
    int* array = new int[userSize];
    
    0 讨论(0)
  • 2021-01-13 11:14

    No, you can't have dynamic allocations like that on the stack (yes, I know, some compilers have extensions for this, but generally speaking the answer is no). You'd want to use malloc or new to dynamically allocate room on the heap. Or of course there are always structures to make life easier, like vector.

    0 讨论(0)
  • 2021-01-13 11:15

    Many of the other answers are correct, but to show your possible choices. Each one will fit into different situations better.

    The first one is most similar to your code. It makes a constant size array and lets the user choose how much of it to use. This is simple, but limiting. It can waste unused space, but has it places to be used. (not likely based on user input though)

    const int SIZE=100;
    int array[SIZE];
    int userSize;
    
    cin >> userSize;
    if (userSize>SIZE){
       cerr << "Array requested too large";
    }
    // use userSize as size of array
    

    The second choice is dynamic memory allocation and using a regular C pointer to keep track of it. If you forget (or are unable) to delete it you have a memory leak.

    cin >> userSize;
    int* array = new int[userSize];
    ...
    delete [] array;
    

    The third choice is writing your own smart pointer, boost, or in C++ 0x a unique_ptr. It will keep track of the pointer for you and delete it when it goes out of scope.

    cin >> userSize;
    unique_ptr<int[]> array(new int[userSize]);
    

    And finally you probably just want a vector. As you hinted to in your question, a vector is probably the way to go here. They are simple, efficient, and commonly used. Learning one stl container makes learning the others easier. You should research which container fits your current problem best. Vector is a good choice most of the time.

    #include <vector>
    ...
    std::vector<int> vec; 
    cin >> userSize;
    vec.resize(userSize);
    
    0 讨论(0)
  • 2021-01-13 11:17

    Yes you can. Here it is:

    int n;
    cout << "enter degree ";
    cin >> n;
    int *arr = new int[n];
    
    0 讨论(0)
提交回复
热议问题