2011-12-13 48 views
2

我對Java非常熟悉,並且允許在那裏使用。但是它看起來不像C++。嘗試分配valuesToGrab = updatingValues;時遇到「無效數組賦值」。C++將int數組賦給一個int數組,其大小相同

//these are class attributes 
int updatingValues[361] = {0}; 
int valuesToGrab[361] = {0}; 

//this is part of a function that is causing an error. 
for (unsigned int i=0; i < 10; i++) { 

    //this fills values with 361 ints, and num_values gets set to 361. 
    sick_lms.GetSickScan(values,num_values); 

    //values has 361 ints, but a size of 2882, so I copy all the ints to an array 
    //of size 361 to "trim" the array. 
    for(int z = 0; z < num_values; z++){ 
     updatingValues[z] = values[z]; 
    } 

    //now I want to assign it to valuesToGrab (another program will be 
    //constantly grabbing this array, and it can't grab it while it's being 
    //populated above or there will be issues 
    valuesToGrab = updatingValues; // THROWING ERROR 
} 

我不希望有通過updatingValues迭代和一個把它添加到valuesToGrab之一,但如果我有我會的。有沒有一種方法可以將它與C++分配到一個函數中?

感謝,

+1

對於C風格數組[memcpy](http://www.cplusplus.com/reference/clibrary/cstring/memcpy/)或[memmove](http://www.cplusplus.com/reference/clibrary/cstring/memmove /) – Joe 2011-12-13 20:59:57

+1

「修整」數組似乎完全沒有必要。修剪,然後複製,雙倍如此。 – 2011-12-13 21:01:16

+1

你真的應該在C&C++中學習內存管理。 – 2011-12-13 21:57:19

回答

6

標準成語在C++複印

#include <algorithm> 
... 
std::copy(values, values+num_values, updatingValues); 

確保updatingValues足夠大,否則你會得到超支和糟糕的事情會發生。

在C++中我們通常使用std :: vector來完成這類任務。

#include <vector> 
... 
std::vector<int> updatingValues=values; //calls vectors copy constructor 

我向量做一切的陣列做(包括在C++ 11靜態initalization),但有一個良好定義的接口。與迭代器,大小,空,調整大小,push_back和更多。

http://en.cppreference.com/w/cpp/algorithm/copy

http://en.cppreference.com/w/cpp/container/vector

編輯 還值得一提的是,你可以結合矢量和陣列。

std::vector<int> vect(my_array, my_array+10); 
//or 
std::vector<int> another_vector; 
... 
another_vector.assign(my_array, my_array+10);//delayed population 

,反之亦然

std::copy(vect.begin(), vect.end(), my_array); //copy vector into array. 
2

在C++中,慣用的容器代替陣列的使用是std::vector。使用vector或使用數組,您可以使用<algorithm>標題中的std::copy()函數,這是在C++中複製任何類型容器的首選方法。隨着vector

std::vector<int> updatingValues, valuesToGrab; 

// Ensure the vector has sufficient capacity to accept values. 
updatingValues.resize(361); 

// Copy values from the array into the vector. 
std::copy(values, values + 361, updatingValues.begin()); 
//  Source begin & end; Destination begin. 

// Copy one vector to another. 
valuesToGrab = updatingValues; 

使用數組:

std::copy(valuesToGrab, valuesToGrab + 361, updatingValues); 

再次剛剛數組,如果你要更多的空調風格,您可以使用C標準庫函數memcpy(),從<cstdlib>

memcpy(valuesToGrab, updatingValues, 361 * sizeof(int)); 
//  Destination; Source;   Number of bytes. 

memcpy()(和它的堂兄弟,memmove()),你必須小心元素的大小你在複製;如果你說的361而不是361 * sizeof(int),你會複製361字節,而不是361 int的字節值 - 這是一個很大的區別。

-1

請記住,數組是作爲C和C++中的指針實現的。

特別是堆棧中的數組可以被視爲一個指向內存中具有您請求的數組容量的常量位置的指針。該內存在堆棧中。當您嘗試使用valuesToGrab = updatingValues時,可以將此視爲嘗試將updatingValues的地址複製到變量valuesToGrab。這是不是嘗試進行深層複製,您似乎在嘗試。但是,valuesToGrab指向內存中的常量位置,無法更新。該標準更具體一些,並明確禁止數組的分配,這就是爲什麼你會得到你所看到的具體錯誤。

您將需要使用循環或類似std::copy或C的memcpy將值從一個陣列複製到另一個。

2

首先,我認爲這不會做你想要的,因爲valuesToGrab = updatingValues;會覆蓋你的valuesToGrab外循環的每個週期。

假設你沒有想,雖然做到這一點,你不想更改爲矢量:

std::copy(updatingValues, updatingValues+361, valuesToGrab); 

將做到這一點。你可以像任何std ::算法中的std :: container一樣處理一個正常的數組,這些指針被視爲隨機訪問迭代器。

雖然重新思考你的設計,但不應該需要「修剪」,而且你可能不需要複製。

相關問題