#include#include using namespace std; int main() { vector intArray = {1,2,3,4,5}; cout << "Before resizing: " << intArray.size(); intArray.resize(10); cout << "\nAfter resizing: " << intArray.size(); return 0; }
#includeIn this example, we have created an integer array named array that is initialised with 5 elements. We have calculated the size of the array using the sizeof operator. We have then printed the size of the array before resizing it. Then we have created a new array of size 10 using new and dynamically allocated memory using the new keyword. We have then copied the contents of the old array to the new array and filled the rest of the array with zeros. We have then deleted the old array using delete[] and assigned the new array to the old array pointer. Finally, we have printed out the new size of the array after resizing it. Package library: Standard Template Library (STL)using namespace std; int main() { int array[] = {1,2,3,4,5}; int arraySize = sizeof(array)/sizeof(array[0]); cout << "Before resizing: " << arraySize; arraySize = 10; int *newArray = new int[arraySize]; for (int i = 0; i < arraySize; i++) { if (i < sizeof(array)/sizeof(array[0])) { newArray[i] = array[i]; } else { newArray[i] = 0; } } delete[] array; array = newArray; cout << "\nAfter resizing: " << arraySize; return 0; }