如果我有一个大小为 MAX_SIZE
的数组,并且只占用了 20 个索引,您如何使它在 itemList[20]
之后停止打印 0? (我正在从一个文本文件中读入)
const int MAX_SIZE = 1000;
item itemList[MAX_SIZE];
for(int i= 0; i<MAX_SIZE;i++)
{
itemList[i].Print(); //prints members in item
if(i==19) // I used this just to see what I was printing properly
{ //I know it is bad practice so I would like an alternative.
break; //Also, it is only possible if you have access to the text file.
}
}
最佳答案
您可以执行基本检查:
if(itemList[i].Function() == 0) break;
关于c++ - 如何在索引为空时停止遍历数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448997/