#include "iostream.h"void main() { int a,b; cout<<"input two numbers:"; cin>>a>>b; int *c=new int[a*b]; for(int i=0;i<a;i++) for(int j=0;j<b;j++) cin>>c[i][j];//输入 for(i=0;i<a;i++) { for(int j=0;j<b;j++) cout<<c[i][j]<<" ";//打印在屏幕上 cout<<endl;//每输出一行则换行。 } }以上是用C++标准写的。本人是在网吧里写的,未调试,有错误在所难免,但思路基本上就是这样了。(注:在输入数组值时可以更友好些,这些代码就楼主自己完成吧。)
发现三个错误,我不知道怎么处理 : error C2109: subscript requires array or pointer type : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable : error C2109: subscript requires array or pointer type Error executing cl.exe.678.exe - 3 error(s), 0 warning(s)
int *c=new int[a*b];改为int *c=new int[a][b];试试
firefox_st(火狐狸)的挺不错,但为了好用,我改进了一下,以后就可以直接访问v[i][j]的二维数组了: int **v; v = new int *[a]; *v = new int[b]; for(int k=0; k<a; k++) *(v+k) = new int[b]; for(int i=0; i<a; i++) for(int j=0; j<b; j++) v[i][j] = 0; stonespace(stonespace)的向量类也很好,可是不适合初学者。
对不住了,上面有一句是多余的,重写: int **v; v = new int *[a]; for(int k=0; k<a; k++) *(v+k) = new int[b]; for(int i=0; i<a; i++) for(int j=0; j<b; j++) v[i][j] = 0;
回 lw_yx(刚学会走,还不想跑.这就是我^-^) “int *c=new int[a*b];改为int *c=new int[a][b];试试” 好象不对,问题更多 error C2540: non-constant expression as array bound D:\vc\678.cpp(8) : error C2440: 'initializing' : cannot convert from 'int (*)[1]' to 'int *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
class CLineVector:public CArray<c1,c2>
{
};动态二维数组:CArray<CLineVector,CLineVector&> xxx
区区作业嘛,可能用不着这些吧
int *c;
c=(int *)malloc(a*b*sizeof(int));同样的效果,做作业刚好,呵呵。
{
public:
CLineVector & operator=(const CLineVector & object);
public:
CLineVector();
CLineVector(const CLineVector&);
};二维数组:
vector<CLineVector> xxx;
{
int a,b;
cout<<"input two numbers:";
cin>>a>>b;
int *c=new int[a*b];
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
cin>>c[i][j];//输入 for(i=0;i<a;i++)
{
for(int j=0;j<b;j++)
cout<<c[i][j]<<" ";//打印在屏幕上
cout<<endl;//每输出一行则换行。
}
}以上是用C++标准写的。本人是在网吧里写的,未调试,有错误在所难免,但思路基本上就是这样了。(注:在输入数组值时可以更友好些,这些代码就楼主自己完成吧。)
: error C2109: subscript requires array or pointer type
: error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable
: error C2109: subscript requires array or pointer type
Error executing cl.exe.678.exe - 3 error(s), 0 warning(s)
int **v;
v = new int *[a];
*v = new int[b];
for(int k=0; k<a; k++)
*(v+k) = new int[b];
for(int i=0; i<a; i++)
for(int j=0; j<b; j++)
v[i][j] = 0;
stonespace(stonespace)的向量类也很好,可是不适合初学者。
int **v;
v = new int *[a];
for(int k=0; k<a; k++)
*(v+k) = new int[b];
for(int i=0; i<a; i++)
for(int j=0; j<b; j++)
v[i][j] = 0;
“int *c=new int[a*b];改为int *c=new int[a][b];试试”
好象不对,问题更多
error C2540: non-constant expression as array bound
D:\vc\678.cpp(8) : error C2440: 'initializing' : cannot convert from 'int (*)[1]' to 'int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
wacky(笨笨狗) 的方法中应该怎么解决释放空间呢?
for(int i=0; i<a; i++)
delete v[i];
delete v;
* (matrix + n * a + b) = 100;int k = * (matrix + n * a + b);
...
释放:
free(matrix)
int ****...v = new int ***...[a];//定义n级指针;for(int i=0; i<a; i++)
for(int j=0; j<b; j++)
for(int k=0; k<c; k++)
......
v[i][j][k]...[x-1] = new int[x];
//赋初始值:
for(i=0; i<a; i++)
for(j=0; j<b; j++)
for(k=0; k<c; k++)
......
v[i][j][k]...[x] = 0;
//释放空间:
for(i=0; i<a; i++)
for(j=0; j<b; j++)
for(k=0; k<c; k++)
......
delete v[i][j][k]...[x-1];
......
delete v[i];
delete v;
wacky(笨笨狗) 和 chzy00(逸少) 的主意到是很不错啊学习ing
如下(以int为例):
void createtwodimention(int _width,int _height)
{
int **array;
int *p;
if((p=new int[_width*_height])==NULL)
{
cout<<"Memory allocate error."<<endl;
exit(1);
} if((array=new int* [_height])==NULL)
{
cout<<"Memory allocate error."<<endl;
exit(1);
} for(int i=0;i<_height;i++)
array[i]=p+i*_width;
}当然,你可以在里面初始花数组!!
有什么意见可以指出,我虚心接受.请不要随意中伤别人.我看你就是那种眼高手低的那种人,没资格对别人指指点点.(你算什么东西啊)
我的mail:[email protected],有问题大家一起讨论吧。