簡體   English   中英

C ++動態分配數組

[英]C++ dynamic allocated array

我正在做一些作業,被困在這里。 我正在嘗試編寫一個list_add()函數。 它的第一個功能是將值添加到數組。 它的第二個功能是增加陣列的大小。 因此,它的工作原理很像向量。 如果我做對了,我不知道。 我試圖創建一個比舊數組更大的新動態分配數組,然后將所有值復制到新數組中。

這是正確的方法嗎?

這是主體

int main()
{
    const int N = 7;

    //declaring dynamic array allocation
    int* list = new int[N];

    int used = 0, a_val;
    for(int i=0;i<11;i++)
    {
        list_add(list, used, N, i);
    }

    cout << endl << "Storlek: " << N << endl << endl;
    cout << "Printar listan " << endl;
    for(int i=0;i<used;i++)
    {
        cout << list[i] << ". ";
    }

}

這是功能

bool list_add(int *list, int& space_used, int max_size, int value)
{

    if(max_size-space_used > 0)
    {
        *(list+(max_size-space_used-1)) = value;
        space_used++;
        return true;
    }
    else
    {
        cout << "Increasing size of array!" << endl;
        int new_max_size = space_used+1;
        delete [] list;
        int *list_new = new int[new_max_size];

        for(int i=0; i<new_max_size; i++)
        {
            list_new[i] = i;
            cout << list_new[i] << ". ";
        }
        cout << endl;
        space_used++;
        list = list_new;
        return false;
    }
}

您有正確的想法,但實施過程中可能會使用一些“手肘油脂”

嘗試這個:

保持2 int

容量-您分配的長度
大小-數組的當前結尾

if capacity <= size:
   make new list( size = capacity x 2 )
   memcopy old list into new list -> if you can't memcopy, copy over the data one-by-one
   delete old list
if capacity > size:
   list[size] = value
   size++

http://www.cplusplus.com/reference/clibrary/cstring/memcpy/

讓我驚訝的一個問題是,您沒有在list_add函數的作用域之外更改列表指針的值。 您應該進行一些更改,例如...

bool list_add(int *list, int& space_used, int max_size, int value)

變成

bool list_add(int **list, int& space_used, int max_size, int value)

list = list_new

變成

*list = list_new

否則,我認為您會發現,當您重新分配列表時,從list_add返回后,列表指針仍將指向舊位置。

您的代碼的實現存在四個問題:

  1. 它不會復制列表的元素。
  2. 它並不值分配new_listlist中的變量main
  3. 它從后到前插入值,而不是最后一個值之后
  4. max_size不會更新。 很容易錯過這一點,因為您每次僅將數組的大小增加一。 這樣,每次添加值時都需要分配。 如果將新大小增加一倍,則每次仍將重新分配。

第一個問題可以通過更改list_add的for循環來解決,以便進行復制:

for (int i = 0; i < space_used; i++) {   // this also changed.
    list_new[i] = list[i];
    cout ...
}
// insert the new value (in the front?)
list_new[max_size-space_used-1] = value;     
delete [] list;         // Delete the list afterwards instead of earlier.

第二個問題可以通過返回指向列表的指針來解決。 main功能更改為此:

for (int i = 0; i < 11; i++) {
    list = list_add(list, used, N, i); 
} 

第三個問題可以通過更改此行來解決

list_new[max_size-space_used-1] = value;

list_new[space_used++] = value;

您還應該在此之后刪除space_used++

要查看第四個問題,您應該更改此行

int new_max_size = space_used+1;

int new_max_size = space_used+3;

每次仍將重新分配。 但是,它應該只重新分配兩次。


這是完整的代碼:

#include <iostream>
using std::cout;
using std::endl;

int* list_add(int *list, int& space_used, int& max_size, int value) {
    if (max_size - space_used > 0) {
        list[space_used++] = value;
        return list;
    }
    else {
        cout << "Increasing size of array!" << endl;
        int new_max_size = space_used+1;

        int *list_new = new int[new_max_size];

        for (int i = 0; i < space_used; i++) {
            list_new[i] = list[i];
            cout << list_new[i] << ". ";
        }
        cout << endl;

        list_new[space_used++] = value;
        max_size=new_max_size;

        delete [] list;
        return list_new;
    }
}

int main() {
    int N = 7;

    //declaring dynamic array allocation
    int* list = new int[N];

    int used = 0, a_val;

    for (int i = 0; i < 11; i++) {
        list=list_add(list, used, N, i);
    }

    cout << endl << "Storlek: " << N << endl << endl;
    cout << "Printar listan " << endl;

    for (int i = 0; i < used; i++) {
        cout << list[i] << ". ";
    }
}

我會擔心這一行:

*(list+(max_size-space_used-1)) = value;

還有這個:

list_new[i] = i;

要知道如何解決問題,有很多話要說,但這不是其中之一。

#include <vector>
#include <iostream>

int main() 
{
    std::vector<int> numbers;

    for (int i = 0; i < 11; i++) {
        numbers.push_back(i);
    }

    for (int i = 0; i < numbers.size(); i++) {
        std::cout << numbers[i] << ". ";
    }

    std::cout << "\n";
}

更新 :如上我的另一個答案所示,他的函數在16行中包含至少四個bug。 這是每四行代碼的錯誤。 然后是代碼設計的問題。 例如,數組的大小和數組本身應該在一起。 否則,您不能保證該功能正常運行。

通過使用包含數組指針和數據結構的max_size的struct ,可以解決代碼(2,4)中的兩個問題。 這樣,您必須將兩個變量一起傳遞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM