![]() |
In C++, an array is a collection of elements of the same type placed in contiguous memory locations. In this article, we will learn how to dynamically resize an array in C++. Example: Input: Resizing a Dynamic Array in C++In C++, the size of an array is static, but we can also create a dynamic array in C++ using the new and delete operators. However, there is no direct method to specifically resize these dynamic arrays. To do that, we have to allocate a new memory block of the new size, copy all the elements of the previous array, and then delete the previous array. Approach
C++ Program to Resize a Dynamic Array in C++The below example demonstrates how we can dynamically resize an array in C++. C++
Output
Original array: 10 20 30 40 50 Resized array: 10 20 30 40 50 0 0 0 0 0 Time Complexity: O(N)
|
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |