【发布时间】:2014-12-04 20:27:12
【问题描述】:
我对使用多维数组并不是很熟悉,在这里我试图查看一个元素是否存在于二维数组中,如果存在,我需要某种指示。
// initialize an array 3x3
int matrix[3][3];
bool found = false;
// The matrix will be loaded with all 0 values, let's assume this has been done.
// Check if there are any 0's left in the matrix...
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
if(matrix[x][y] == 0){
break; // << HERE I want to exit the entire loop.
}else{
continue; // Continue looping till you find a 0, if none found then break out and make: found = true;
}
}
}
【问题讨论】:
-
使用
found变量。删除continue;没有你的循环就不会做任何其他事情。 -
你需要在 break 之前设置一些标志(例如,布尔变量)。然后,检查内部循环外部的标志,并在必要时再次中断。这将打破两个循环。你不需要那个 continue 语句。
-
把它变成一个函数,找到后直接返回,代码会更简洁。