簡體   English   中英

將對象數組推回向量 c++

[英]pushing back an array of objects into a vector c++

我有一個類,它接受一個數組並在隨機位置用相同的數字重新填充數組。 我現在需要生成一個列表,該列表生成 10 個隨機數組列表,我相信我已經完成了。 我通過創建我的類對象數組來完成此操作,如我的代碼所示。

class numbers{
    private:
    int indexCount;

    public:
    void swap (int *a, int *b)  
{  
    int temp = *a;  
    *a = *b;  
    *b = temp;  
}  
void printArray (int arr[], int n)  
{  
    for (int i = 0; i < n; i++)  
    cout << arr[i] << " ";  
    cout << "random calls: " << indexCount <<endl; 

}  
  void randomize (int arr[], int n)  
{  

   indexCount=0;
    for (int i = n - 1; i > 0; i--)  
    {  
        int j = rand() % (i + 1);  
        indexCount++;
        swap(&arr[i], &arr[j]);  

    }  
}


};

       int main()
       {

        srand (time(NULL));
        vector <int> list;

            int i;
            int arr[] = {1, 2, 3, 4, 5, 6, 0, 0, 0};  
            int n = sizeof(arr) / sizeof(arr[0]);  
           numbers a[10]; 
            for (i=0; i <10;i++)
            {
            a[i].randomize(arr,n);
            a[i].printArray(arr,n); 
        //  list.push_back(a[i]);

            }


return 0;


       }

我想要做的是將包含每個列表的對象 a 數組推回到列表向量中,這樣 list[1] 將包含 {1,0,2,3,0,0,6,5,4}而 list[2] 將包含另一組數字。

我的問題:我怎樣才能將包含數字列表的對象數組推回到我的向量中。

例如:輸入:{1,2,3,4,5,6,0,0,0} 輸出向量列表[0] 包含{1,0,2,3,0,0,6, 5,4} list[1] 包含 {0,0,1,6,0,4,3,5,2} ... list[9] 包含 {1,0,2,0,3,4,6 ,5,0}

不能使用標准數組(例如int a[10] )對向量進行模板化。 它可以使用指針(例如int * )進行模板化,也可以使用另一個std::vector進行模板化。

如果您有一個數組,並且希望將該數組的值存儲在向量的單個索引中,則需要將數組的元素復制到向量中的容器中。 對於std::vector<int*>這看起來像這樣:

    std::vector<int*> array_vec;
    int numbers[] = {1, 2, 3, 4, 5, 6, 0, 0, 20};
    for (std::size_t i = 0; i < 10; i++)
    {
        int* temp = new int[sizeof(numbers)/sizeof(numbers[0])];
        std::copy(&numbers[0], &numbers[9], temp);
        array_vec.push_back(temp);

        // change numbers so that it's different and we can see that reflected in the output
        numbers[8] = i;
    }

    // print the elements from the vector
    for (auto a : array_vec)
    {
        std::cout << "{ ";
        for (std::size_t i = 0; i < 9; i++)
        {
            std::cout << a[i] << " ";
        }
        std::cout << "}\n";
    }

    // we need to delete the memory we allocated
    for (auto a : array_vec)
        delete[] a;

注意:我不推薦這種方法,因為它需要使用new[]delete[] 在現代 C++ 中,您沒有理由像這樣手動管理內存,尤其是作為初學者。

更好的解決方案是使用std::vector<std::vector<int>> 無需手動內存管理。 您可以利用std::vector<int>構造函數,該構造函數為int兩個迭代器,一個開始迭代器和一個結束迭代器,以及emplace_back (而不是push_back ):

    std::vector<std::vector<int>> vector_vec;
    int numbers[] = {1, 2, 3, 4, 5, 6, 0, 0, 20};
    for (std::size_t i = 0; i < 10; i++)
    {
        // create a new std::vector<int> in vector_vec by calling its iterator constructor
        vector_vec.emplace_back(numbers, numbers + 9);

        numbers[8] = i;
    }

    // print the elements from the vector
    for (auto& v : vector_vec)
    {
        std::cout << "{ ";
        for (auto i : v)
        {
            std::cout << i << " ";
        }
        std::cout << "}\n";
    }

注意:現在不需要手動delete[]內存,因為std::vector為我們管理內存。


此答案並未解決您問題中的課程問題。 如果您有一個問題,則必須在另一個問題中回答。

如果您對std::vector類、其構造函數或其成員函數有疑問,請參閱std::vector

暫無
暫無

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

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