我怎樣才能傳遞二維數組到一個函數,並使用它像myArray [我] [J],但不知道該數組內的大小數組?C++和根,傳遞二維數組功能,並在TGraph中使用它
我可以知道主體內的大小。
我想用這樣的:
TGraph *myGraph = new TGraph(nValues, myArray[0][j], myArray[1][j]);
// I'll not use a loop for j, since TGraph receives all the values in the array,
like "x values" and "y values"
如果我這樣做,這樣它的工作原理,但我會傳遞給函數col1和col2的是兩個一維數組:
main() {
...
graphWaveTransmittance("a", nValues, Col1, Col2,
" Au ", "Evaporated", "thickness 5nm", kGreen+1);
...
}
void graphWaveTransmittance(char *n, int nValues, float Param1[], float Param2[],
char *title, char *header, char *entry, Color_t color) {
TGraph *myGraph = new TGraph(nValues, Param1, Param2);
...
}
的陣列:
float valuesArray[nCol][nValues];
for(int y=0; y<nValues; y++){
for (int i=0; i<nCol; i++) {
valuesArray[i][y] = values[i][y];
}
i=0;
}
注:我已完成它像這樣,因爲值[] []是值的數組被讀從一個文本文件。在閱讀文件之前,我不知道需要多少行。有了這第二個數組(valuesArray [] []),我可以使它只有讀取的值的數量的大小。首先,我已將所有值[] []的值設置爲「-1」,並且它的大小非常大。然後我計算了行數,並將values用於valuesArray [] []。這是價值觀的第一陣列(大個):其他
const int nCol = countCols;
float values[nCol][nLin];
// reads file to end of *file*, not line
while(!inFile.eof()) {
for(int y=0; y<nLin; y++){
for (int i=0; i<nCol; i++) {
inFile >> values[i][y];
}
i=0;
}
}
一個問題,我已經看到了,「雖然(inFile.eof()!)」不應該被使用。我可以用什麼來代替? (我不知道從.txt文件的行此時的總數)
在一個.txt導入在列中的值,直到我現在有:
vector<vector<float> > vecValues; // your entire data-set of values
vector<float> line(nCol, -1.0); // create one line of nCol size and fill with -1
bool done = false;
while (!done)
{
for (int i = 0; !done && i < nCol; i++)
{
done = !(inFile2 >> line[i]);
}
vecValues.push_back(line);
}
這樣做的問題是該值是像vecValues [值] [從.txt的列號] 我想有vecValues [從.txt列值] [值]。
我該如何改變它?
我是從這樣的文件中讀取:
main() {
...
vector < vector <float> > vecValues; // 2d array as a vector of vectors
vector <float> rowVector(nCol); // vector to add into 'array' (represents a row)
int row = 0; // Row counter
// Dynamically store data into array
while (!inFile2.eof()) { // ... and while there are no errors,
vecValues.push_back(rowVector); // add a new row,
for (int col=0; col<nCol; col++) {
inFile2 >> vecValues[row][col]; // fill the row with col elements
}
row++; // Keep track of actual row
}
graphWaveTransmittance("a", nValues, vecValues, " Au ",
"Evaporated", "thickness 5nm", kGreen+1);
// nValues is the number of lines of .txt file
...
}
//****** Function *******//
void graphWaveTransmittance(char *n, int nValues,
const vector<vector <float> > & Param, char *title, char *header,
char *entry, Color_t color) {
// like this the graph is not good
TGraph *gr_WTransm = new TGraph(nValues, &Param[0][0], &Param[1][0]);
// or like this
TGraph *gr_WTransm = new TGraph(Param[0].size(), &Param[0][0], &Param[1][0]);
注:TGraph可以接受的花車,我以前的陣列是花車
你知道爲什麼圖表顯示不正確?
謝謝
爲什麼不顯示**還有你的「2D陣列」? –
@Doms我改變了最初的問題。提到TGraph,你知道什麼是錯的嗎? – JMG