함수에 2D 배열을 전달하고 myArray [i] [j]와 같이 사용하려면 어떻게해야합니까?2D 배열을 함수로 전달하여 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"
나는이 작동하지만 두 개의 1D 배열 인 기능 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;
}
참고 값 [] []하여 판독 값 배열이기 때문에 I는 다음과 같이하여 만들었다 텍스트 파일에서. 파일을 읽기 전에 얼마나 많은 행이 필요한지 알 수 없습니다. 이 두 번째 배열 (valuesArray [] [])을 사용하면 읽을 수있는 값의 크기만큼만 만들 수 있습니다.
먼저 모든 값을 [-1]로 값 [] []에 넣었으며 크기는 매우 큽니다. 그런 다음 줄 수를 계산하고 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