2

How would I loop through a multidimensional array? Say we had something like this:

class blah
{
    public:
    blah();
    bool foo;
};

blah::blah()
{
    foo = true;
}

blah testArray[1][2];
testArray[1][0].foo = false;

How would I go about looping through testArray to find which one of foo is false?

4
  • 1
    Same way you loop through any other array, except the body of the loop will be another loop Commented Nov 13, 2011 at 6:08
  • 2
    testArray[1][0] is out of range for your declared array Commented Nov 13, 2011 at 6:14
  • 1
    It's pseudocode. Doesn't matter. You can even fly in pseudocode. Commented Nov 13, 2011 at 6:16
  • @Lemmons: I was only pointing it out because if you were testing with that and couldn't find the false value, that may have been your problem. Commented Nov 13, 2011 at 6:22

5 Answers 5

8

This one isn't dependent on magic numbers:

#include <cstddef>
for (size_t x = 0; x < sizeof(*testArray) / sizeof(**testArray); ++x)
for (size_t y = 0; y < sizeof(testArray)  / sizeof(*testArray);  ++y) {
  if (testArray[x][y].foo == false) {

  }
}

Having x in the outer loop leads to better caching.

Sign up to request clarification or add additional context in comments.

5 Comments

+1...but maybe add #include <cstddef> and using std::size_t? It's a pet peeve of mine. What if (not likely, and not in the question) the rows are not all the same length?
@keith.layne I'll add the header, but the std isn't needed. Can array types even be defined with differing rows?
I feel dumb now, didn't know it was declared in the global namespace. I'm checking on your question.
I guess not, but you could make an array of pointers that are arrays of different lengths, like so: int* a[2]; a[0] = new int[7]; a[1] = new int[4];. I don't think my point really pertains to anything, I'm just your local devil's advocate. I think your technique would work anyway if you adjusted a little and then ordered the loops oppositely...you'd lose your optimization though. I think that's another good question...won't most compilers reorder that loop anyway for the exact same reason?
Shouldn't the first loop use the stop condition of the second loop and vice versa?
6
class blah
{
    public:
    blah();
    bool foo;
};

blah::blah()
{
    foo = true;
}

int testArrayFirstLength = 1;
int testArraySecondLength = 2;

blah testArray[testArrayFirstLength][testArraySecondLength];
testArray[1][0].foo = false;


for (int i = 0; i < testArrayFirstLength; i++) {
    for (int j = 0; j < testArraySecondLength; j++) {
        if (!testArray[i][j]) {
            blah thing = testArray[i][j]
        }
    }
}

That good? Or were you looking for something else?

Comments

0
int x = 0;
int y = 0;

for( x = 0; x < 1; x++ ){
    for( y = 0; y < 2; y++ ){
       if( testArray[x][y].foo == false ){
           //yeah!
       }
    }
}

Comments

0
for (std::size_t i(0); i != 1; ++i){
    for (std::size_t j(0); j != 2; ++j) {
        if (!testArray[i][j].foo) {
            //testArray[i][j].foo is false
            //Perform the required operation
        }
    }
}

Comments

0

Tested in c++ +14 (using pointers, so be safe)

#include <iostream>
int main()
{
int array[2][2][2]; 
int* pointer=&array[0][0][0]; 
for(int i=0; i<2*2*2; i++)
    {
        std::cout<<*pointer<<std::endl;     
        pointer++; 
    }
}

1 Comment

You didn't use the variable i here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.