How to add element to C++ array?

后端 未结 11 2472
旧时难觅i
旧时难觅i 2020-12-12 23:26

I want to add an int into an array, but the problem is that I don\'t know what the index is now.

int[] arr = new int[15];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;         


        
11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 00:08

    Since I got so many negative feedback I realized my solution was wrong so i changed it.

    int arr[20] = {1,2,3,4,5};
    int index = 5;
    
    void push(int n){
        arr[index] = n;
        index++;
    }
    
    push(6)
    

    New array will contain value from 1 to 6 and you can make function for deletion like this as well.

提交回复
热议问题