c++ - 如何在索引为空时停止遍历数组?

标签 c++ arrays break

如果我有一个大小为 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/

相关文章:

r - 使用R将变量值编码为类

c++ - 使用 lambda 初始化包含 std::function 的类

c# - 从 C# 调用 C++ 函数

android - 如何在 Android 中使用 Room 存储对象数组?

javascript - 通过包含在另一个数组中的对象属性对 Javascript 数组进行排序

javascript - 如何在 JavaScript 中找到换行符并替换为 <br> 元素?

c - 关于C中 "goto"的问题

c++ - 使用预编译的库包

c++ - 从 iostream 读取父类(super class)的子类实例。 >> 运算符如何知道哪个子类?

c - C 中各种字符串初始化的大小差异