<分区>
我需要写这样的程序:
The original elements of the array
20 34 45 2 10
Enter the index and number for insertion
3 12
Elements after insertion
20 34 45 12 2 10
我声明一个数组来存储初始数字:
int array[5] = { 20, 34, 45, 2, 10 };
但是我怎样才能插入到这个数组中呢?
<分区>
我需要写这样的程序:
The original elements of the array
20 34 45 2 10
Enter the index and number for insertion
3 12
Elements after insertion
20 34 45 12 2 10
我声明一个数组来存储初始数字:
int array[5] = { 20, 34, 45, 2, 10 };
但是我怎样才能插入到这个数组中呢?
最佳答案
C++ 中的数组大小固定,因此 5 个整数的数组不能容纳超过 5 个整数,因为这是为其分配的内存量,仅此而已。 您可以研究其他数据结构,如列表和 vector ,或者您可以声明您的数组足够大以容纳您想要的所有对象。 在这里,您可能想要查看链表,它可以在不移动所有其他元素的情况下进行插入。
关于c++ - 如果它的大小是固定的,如何插入到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46478833/