不确定此循环的条件(指针算术)
Unsure of the condition for this loop (Pointer arithmetic)
所以我应该通过评论中的要求编辑一堆代码。我已经得到了大部分,但有一部分特别让我卡住了。它像原来那样循环,但我不知道循环的条件应该是什么,不管我放什么,最后都会崩溃。我想是尺寸为?但我试过的都没用。任何帮助将不胜感激!
原始:int main()
{
const string ID_BASE = "56-123-";
// CHANGE STATIC ARRAY INTO DYNAMIC ARRAY.
const int NUM_PRODUCTS = 5;
Product products[NUM_PRODUCTS];
int i;
//cout << "Enter number of toys: ";
//cin >> numProducts;
....Code to fill products...
//ALTER LOOP TO USE ONLY POINTER OPERATIONS
// (NO SUBSCRIPTS).
for (i = 0; i < numProducts; i++)
{
show(products[i]);
}
我的变化:
int main()
{
const string ID_BASE = "56-123-";
// CHANGE STATIC ARRAY INTO DYNAMIC ARRAY.
int numProducts;
Product *products;
int i;
cout << "Enter number of toys: ";
cin >> numProducts;
products = new Product[numProducts];
...Code to fill products...
//ALTER LOOP TO USE ONLY POINTER OPERATIONS
// (NO SUBSCRIPTS).
for (Product *prodP = products; ; *prodP++)
{
show(*prodP);
}
条件:
prodP !=products+numProducts;
Do it iterator style:
auto begin = products;
auto end = products + numProducts;
for( auto itr=begin; itr!=end; ++itr )
{
show(*itr);
}
也许我在这里错过了一些东西,但是修改后的循环何时结束??
for (Product *prodP = products; ; *prodP++)
{
show(*prodP);
}
只有有限的元素,我认为你超出了数组。
相关文章:
- 移除和删除与向量中的条件匹配的指针
- 函数指针与条件分支
- 根据指针条件初始化std::vector
- 如何使用条件表达式返回对象指针?
- C++可以有条件地向下转换类指针吗
- 智能指针析构函数争用条件
- 基于指针或值类型的条件类型
- 当直接分配时,为什么此功能指针分配起作用,而不是与有条件的操作员一起使用
- 智能指针自定义删除器中的处理条件
- 条件中的指针语法
- C++:如何在 if 语句条件中使用指针
- 为什么包含指针的条件总是返回 true?
- 在条件或循环中分配智能指针
- 在哪些条件下,我们将指针作为函数参数
- 带有原始指针的boost :: shared_ptr变量的条件初始化
- 在 IF(指针)条件内增加指针
- 使用字符指针测试条件
- 条件运算符和数组/返回指向数组的指针
- C++程序在使用 if 条件检查指针是否为 NULL 时崩溃
- 我在这里滥用了指针吗?虽然循环条件似乎无法正常工作