// rememb-o-matic
#include <iostream>
#include <new>
using namespace std;
int main()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int [i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}
为什么他们的支架装在变量i?
p= new (nothrow) int [i];
为什么他们2所for
陈述又是什么一个for
声明做什么呢?
为什么它会删除[] p而不是变量p
?
如果你需要询问有关循环,那么你可能应该[在C++上阅读一本好书](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list )。而这段代码很糟糕。 –
也许一本介绍C++的书会是一个好的开始?在这里有一个很好的建议清单。 –