2013-12-10 61 views
0

我怎樣才能傳遞二維數組到一個函數,並使用它像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可以接受的花車,我以前的陣列是花車

你知道爲什麼圖表顯示不正確?

謝謝

+0

爲什麼不顯示**還有你的「2D陣列」? –

+0

@Doms我改變了最初的問題。提到TGraph,你知道什麼是錯的嗎? – JMG

回答

0

我最近碰到類似的問題。我結束了使用二維vectors,傳遞給函數時不需要知道內部維度。

聲明向量這樣

vector< vector<int> > vec(xRange, vector<int>(yRange, initialValue)); 

雖然與你的尺寸在x維度,yRange與y方向的尺寸更換XRANGE,和初值與要初始化與2D矢量什麼。

在這一點上,你可以訪問或使用

vec[x][y] 

要通過這一個功能更新向量內容,請使用此

void myFunc(std::vector< std::vector<int> >& vec) { 

一定要

#include <vector> 
+0

我試圖使用矢量,但我一直在收到錯誤。但是,以任何方式感謝你 – JMG

+0

你得到什麼錯誤?也許我可以幫忙。 –

+0

我第一次運行腳本時,它確定,但第二次出現:警告:模板對重複定義/opt/local/lib/root/cint/cint/stl/_pair.h:31: 警告:模板reverse_iterator重複定義/opt/local/lib/root/cint/cint/stl/_iterator.h:269: 警告:模板向量重複定義/opt/local/lib/root/cint/cint/stl/_vector.h:44: (無法格式化此文本足夠好:)然後,分段錯誤 – JMG

0

你可以像邁克爾帕克已經提到的那樣使用媒介來解決這個問題。爲了使它在圖中以root方式工作,請注意TGraph需要兩個double或float數組作爲參數。 因此,你必須使用

std::vector<std::vector<double> >std::vector<std::vector<float> >

你的情況。 然後你的函數看起來像:

void drawGraph(const std::vector<std::vector<double> > & data) 
{ 
    TGraph* graph = new TGraph(data[0].size(), &data[0][0], &data[1][0]); 
    //... 

} 

&data[0][0]「轉換」需要通過TGraph的向量陣列。

關於而是採用!inFile.eof()你的第二個問題,我通常直接問閱讀過程中是否有成功的,也就是你的情況:

if(!(inFile >> values[i][y])) 
{ 
//at end of file 
} 

我更喜歡在一個while循環利用這一點,但多數民衆贊成的事的味道。 順便說一下,通過使用矢量,您不再需要預先運行整個文件來計算行數,只需使用push_back(...)即可。

如果你不知道的行數也列數,你可以使用函數getline讀出文件,並確定列數:

std::vector<std::vector<float> > data; 
std::string buffer; 
getline(infile,buffer)//I do not check that the file has at least one line here 
istringstream firstLine(buffer); 
float value; 
while(firstline >> value) 
{ 
    data.push_back(std::vector<float>(1,value)); 
} 

while(getline(infile,buffer)) 
{ 
    istringstream line(buffer); 
    float value; 
    unsigned int counter = 0; 
    while(line >> value) 
    { 
     data[counter].push_back(value)); 
     counter++; 
    } 
} 

注意,這需要:

#include<sstream> 

並且假設列的數量不會改變文件大小。

+0

如何正確導入帶有一些列和很多行的文本文件?我要改變最初的問題,把一些代碼 – JMG

+0

我不能使用if(!(inFile >> values [i] [y])),因爲我不知道行數或列數。數組我有它,但我想刪除它 – JMG

+0

我已經將該向量傳入函數,但該圖不是它應該如何,你知道發生了什麼嗎? 我要把疑問放在「問題」 – JMG