I need to do the equivalent of the following C# code in C++
Array.Resize(ref A, A.Length - 1);
How to achieve this in C++?
There is no way to resize an array. You can simply create a new array of size 2, then copy all the data from the previous one to the new one. realloc does it for you with dynamic memory.
You cannot resize an array in C#, but using Array. Resize you can replace the array with a new array of different size.
Resize<T>(T[], Int32) Method.
You cannot resize array, you can only allocate new one (with a bigger size) and copy old array's contents. If you don't want to use std::vector
(for some reason) here is the code to it:
int size = 10; int* arr = new int[size]; void resize() { size_t newSize = size * 2; int* newArr = new int[newSize]; memcpy( newArr, arr, size * sizeof(int) ); size = newSize; delete [] arr; arr = newArr; }
code is from here http://www.cplusplus.com/forum/general/11111/.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With