How to resize array in C++?

前端 未结 6 553
感情败类
感情败类 2020-11-29 09:16

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++?

相关标签:
6条回答
  • 2020-11-29 09:45
    1. Use std::vector or
    2. Write your own method. Allocate chunk of memory using new. with that memory you can expand till the limit of memory chunk.
    0 讨论(0)
  • 2020-11-29 09:48

    Raw arrays aren't resizable in C++.

    You should be using something like a Vector class which does allow resizing..

    std::vector allows you to resize it as well as allowing dynamic resizing when you add elements (often making the manual resizing unnecessary for adding).

    0 讨论(0)
  • 2020-11-29 09:49

    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/.

    0 讨论(0)
  • 2020-11-29 09:51

    You can do smth like this for 1D arrays. Here we use int*& because we want our pointer to be changeable.

    #include<algorithm> // for copy
    
    void resize(int*& a, size_t& n)
    {
       size_t new_n = 2 * n;
       int* new_a = new int[new_n];
       copy(a, a + n, new_a);
       delete[] a;
       a = new_a;
       n = new_n;
    }
    

    For 2D arrays:

    #include<algorithm> // for copy
    
    void resize(int**& a, size_t& n)
    {
       size_t new_n = 2 * n, i = 0;
       int** new_a = new int* [new_n];
       for (i = 0; i != new_n; ++i)
           new_a[i] = new int[100];
       for (i = 0; i != n; ++i)
       {
           copy(a[i], a[i] + 100, new_a[i]);
           delete[] a[i];
       }
       delete[] a;
       a = new_a;
       n = new_n;
    }
    

    Invoking of 1D array:

    void myfn(int*& a, size_t& n)
    {
       // do smth
       resize(a, n);
    }
    

    Invoking of 2D array:

    void myfn(int**& a, size_t& n)
    {
       // do smth
       resize(a, n);
    }
    

    The declaration of this function should be earlier than one of myfn. And its definition too. These functions were tested and they work correctly.

    0 讨论(0)
  • 2020-11-29 09:53

    The size of an array is static in C++. You cannot dynamically resize it. That's what std::vector is for:

    std::vector<int> v; // size of the vector starts at 0
    
    v.push_back(10); // v now has 1 element
    v.push_back(20); // v now has 2 elements
    v.push_back(30); // v now has 3 elements
    
    v.pop_back(); // removes the 30 and resizes v to 2
    
    v.resize(v.size() - 1); // resizes v to 1
    
    0 讨论(0)
  • 2020-11-29 10:00

    You cannot do that, see this question's answers. You may use std:vector instead.

    0 讨论(0)
提交回复
热议问题