简体   繁体   English

引用2D数组中的整个数组行

[英]Referring to an entire row of arrays in a 2D array

I have a 2D array of numbers right now and what i need to do is figure out how to refer to an entire "row" of them with just one name... 我现在有一个二维数字数组,我需要做的是弄清楚如何仅用一个名字来指代它们的整个“行”。

What i am trying to do is to make each "row" a TBranch on a TTree in a program called ROOT. 我正在尝试做的是在名为ROOT的程序中,使每个“行”成为TTree上的TBranch。 Each row is a list of numbers corresponding to the data in all the bins on a single histogram and each column is filled with the numbers corresponding to a specific bin (ie: bin 3) in every histogram (if that makes sense). 每行是与单个直方图上所有bin中的数据相对应的数字的列表,并且每一列均填充有与每个直方图中的特定bin(即bin 3)相对应的数字(如果有意义)。 I just need to find a way to separate the data by histogram/row and treat those as their own individual thing if that is possible. 我只需要找到一种方法即可按直方图/行将数据分离,并在可能的情况下将其视为自己的东西。 I apologize if this is not coherent! 如果不连贯,我深表歉意!

I don't really understand the second part of your question so i'll just adress how to access rows in a 2D array (aka a matrix). 我不太了解您问题的第二部分,因此我只解决如何访问2D数组(又称矩阵)中的行的问题。 If this isn't what you asked you'll have to expand your question. 如果这不是您的要求,则必须扩大您的问题。

Multidimensional arrays are usually set up as arrays containing other arrays (and so on). 多维数组通常设置为包含其他数组的数组(依此类推)。 A 2D array could look like this: 2D数组可能如下所示:

std::vector<std::vector<int> > myMatrix;
for (int y = 0; y < LIMIT_Y; ++y) {
    std::vector row;
    for (int x = 0; x < LIMINT_X; ++x) {
        row.push_back(0);
    }
    myMatrix.push_back(row);
}

This example will yield you a 2D array filled with zeros. 本示例将为您生成一个填充有零的2D数组。 However you specify if myMatrix holds rows which hold values for columns or if myMatrix holds columns which hold values for rows. 但是,您可以指定myMatrix保存包含列值的行还是myMatrix保存包含行值的列。 In this case I chose it to contain rows. 在这种情况下,我选择它包含行。 You can know acces specific rows of the matrix by myMatrix[i] which will return the i-th row as an std::vector<int> 您可以通过myMatrix[i]知道矩阵的特定行,这将以std::vector<int>返回第i行

If you have created a 2d array like so: 如果您像这样创建了一个二维数组:

char** arr;
// fill up arr with elements
int rows = 20, columns = 50;
arr = (char**) malloc (rows * columns * sizeof(char));

and you want to refer to, say row No5, you do: 并且您要引用第5行,您可以执行以下操作:

arr[4] // here, the index 4 refers to the entire 5th row of arr

For example you can pass arr[4] to a function which expects a pointer argument. 例如,您可以将arr[4]传递给需要指针参数的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM