C++和Root,将2D数组传递给函数并在TGraph中使用它
C++ and Root, passing 2D array to function and use it in TGraph
如何将2D数组传递到函数中,并像myArray[I][j]一样使用它,但不知道函数中该数组的大小?
我能知道主管道里面的尺寸。
我想这样使用它:
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,这是两个1D数组:
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;
}
注意:我之所以这样做,是因为values[][]是一个数组,其中的值是从文本文件中读取的。在阅读这个文件之前,我不知道需要多少行。使用第二个数组(valuesArray[][]),我可以使它的大小与读取的值的数量相同。
首先,我把所有的值都放在带有"-1"的值[][]中,它的大小非常大。然后我计算了行数,并将该值用于值Array[][]。这是第一个有值的数组(较大的一个):
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;
}
}
还有一个问题,我看到不应该使用"while(!inFile.of())"。我可以用什么代替?(目前我不知道.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[value][column number from.txt]我想要vecValues[列号从.txt][value].
我该怎么改?
我正在阅读这样的文件:
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可以接受float,我以前的数组是float
你知道为什么这个图显示不正确吗?
感谢
我最近遇到了类似的问题。我最终使用了二维向量,当传递给函数时,它不需要知道内部维度。
像这样声明向量
vector< vector<int> > vec(xRange, vector<int>(yRange, initialValue));
将xRange替换为x维度的大小,将yRange替换为y方向的大小,并将initialValue替换为要初始化2d向量的值。
此时,您可以使用访问或更新矢量内容
vec[x][y]
要将其传递给函数,请使用此
void myFunc(std::vector< std::vector<int> >& vec) {
确保
#include <vector>
正如Michael Parker已经提到的那样,您可以使用向量来解决这个问题。要使它在根目录下与图形一起工作,请注意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
}
我更喜欢在一段时间内使用这个,但这是品味的问题。顺便说一句,通过使用矢量,您不再需要提前运行整个文件来计算行数,只需使用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>
并且这假设列的数量不会随着文件大小而改变。
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- C++和Root,将2D数组传递给函数并在TGraph中使用它