我如何循环遍历多维数组?说我们有这样的事情:
class blah
{
public:
blah();
bool foo;
};
blah::blah()
{
foo = true;
}
blah testArray[1][2];
testArray[1][0].foo = false;
Run Code Online (Sandbox Code Playgroud)
我将如何循环testArray查找哪一个foo是假的?
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]
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么好?或者你在寻找别的东西?
这个不依赖于幻数:
#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) {
}
}
Run Code Online (Sandbox Code Playgroud)
有x外循环导致更好的缓存.