How to get size c++ dynamic array

前端 未结 5 1936
南方客
南方客 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 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.

提交回复
热议问题