How to get size c++ dynamic array

前端 未结 5 1924
南方客
南方客 2020-12-02 02:31

I\'m studying C++ and I need to create structure Airplane and work with it.

My structure Airplane.h

#include \"stdafx.         


        
相关标签:
5条回答
  • 2020-12-02 02:51

    One approach that i use when i work with dynamic arrays, is to pass the array as a reference to the function. This will help you to keep the size information of the array.

    For Example:

    string list1[3] = {
        "item1",
        "item2",
        "item3"
    };
    
    string list2[2] = {
        "item1",
        "item2"
    };
    
    template<typename T, size_t N>
    void PrintItems(T(&items)[N]) {
        for (int i = 0; i < N; i++) {
            cout << items[i] << endl;
        }
    }
    
    int main() {
        PrintItems(list1);
        PrintItems(list2);
    }
    

    In the previous example, N stores the correct size information for the array. More information here

    0 讨论(0)
  • 2020-12-02 03:01

    you could probably make a dynamic array with 0 items, make an int counter, make a while loop with getline as a statement, while (getline(cin, string_var) != SomeText) /* SomeText = some kind of text so the user show you there are not going to be any more inputs*/, what you were going to do it in the for do it in the while and at the end of the while increase the counter by one, i++. And about the access to the array, if your dynamic array has 0 items, then SomeDynamicArray[1].something = SomeValue will just add a second item to the array and the "something" of that item will be equal to SomeValue.

    type *ArrayPointer = new type[0];
    string StringVar;
    int i = 0;
    while (getline(cin, StringVar) != "Text that show there are not going to be any more inputs") {
         /*code*/
         i++;
    } 
    

    idk if it will work on your case, but try it if you want. Also, about everyone saying about vectors, as I know at least, vectors are slower and spend more memory, bcs they double there size instead of just increasing it every time needed. I hope I will be some help.

    0 讨论(0)
  • 2020-12-02 03:06

    The problem with entering type is that the input buffer contains the new line character after entering n. You should use member function ignore to clear the buffer before using function getline.

    As for your second question then in general you should track the size of a dynamically allocated array yourself. Or you can set the last element of the array as NULL and use it as a sentinel.

    0 讨论(0)
  • 2020-12-02 03:10

    1) Ommiting the irrelevant, this is basically what you got:

    cin >> n;
    getline(cin, type);
    

    operator>> leaves a new-line character in the input buffer and that's the first character that getline sees. Since '\n' is the default line delimiter, you get an empty line. To fix it call cin.ignore() before you call getline to discard the '\n'.

    2) If you wish to stick with raw pointers, passing the size as a parameter is your only choice. Switch to std::vector and you get size() method that you can query at any time.

    0 讨论(0)
  • 2020-12-02 03:13

    "How to get size of dynamic array in functions? Because it seems the passing size of array n in every function is the wrong way."

    Yet it is the only way when you use dynamically allocated C-style array.

    If you want to avoid sending the size explicitly then pass some object that wraps this raw memory buffer and provides other means of retrieving the size. The most reasonable solution here would be using std::vector<Airplane>.

    0 讨论(0)
提交回复
热议问题