http://blog.bc-cn.net/user1/121/archives/2006/2172.shtml
下面给一个动态开辟一维空间的例子:
#include <iostream>
using namespace std;
int main()
{
int n;
int * p; // to receive the addresse, where a dynamic array allocated
// so that you can work with this dynamic array
// pay attention: here is one dimension array
cout<<"Please enter the number of elements, that you want an one dimension array hat: ";
if(cin>>n)
{
p = new int[n];
// now you have allocated the space for your one dimension array
// p is here the first address of this array
// you can write someting there
// and make some changement
// or display them.
// for example I write something in this array
for(int i = 0; i<n; i++)
{
p[i] = i;
}
// and maybe some statements will be here
// --- some code ---
// and now some code to change this array
// for example inverse
for(int i = 0; i<(n>>1); i++)
{
int temp = p[i];
p[i] = p[n-i-1];
p[n-i-1] = temp;
}
// now you maybe want show this array
for(int i = 0; i<n; i++)
{
cout<<p[i]<<" ";
}
}
// you have done everything with your program, now you want leave.
// Caution: Never forget to free the dynamic allocation
delete [] p;
system("pause");
return 0;
}
自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!