简体   繁体   中英

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. 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). 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). 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:

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. However you specify if myMatrix holds rows which hold values for columns or if myMatrix holds columns which hold values for rows. 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>

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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