C/C++ 动态数组的创建的实例详解

2025-05-27 0 27

C/C++ 动态数组的创建的实例详解

C++语言中,二维动态数组主要使用指针的方法建立,以建立一个整数二维数组为例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33
#include<iostream>

#include<string>

#include<malloc.h>

using namespace std;

int main(int argc,char **argv)

{

///*int a[2][3]={{1,2,3},{4,5,6}};

//cout<<sizeof(a+1)<<endl;*/

//int a=4;

//int **pp;

//pp=(int **)malloc(sizeof(int*)*a);

//int aa[5][1]={1,2,3,4,5};

//return 0;

int column,row; cout<<"输入二维数组的行数和列数"<<endl;

cin>>row>>column;

int **array;

array = (int **)malloc(sizeof(int *)*row);

for(int i=0;i!=row ; i++)

array[i]=(int *) malloc(sizeof(int )*column);

cout<<"输入二维数组"<<endl;

for(int j=0 ; j !=row ; j++)

{for(int k=0 ; k !=column ; k++) {cin>>array[j][k]; } }

cout<<"输入的二维数组为"<<endl;

for( int j=0 ; j !=row ; j++ )

{ for(int k=0 ; k !=column ; k++)

{cout<<array[j][k]<<" "; }

cout<<endl; }

//释放空间   

for(int i=0 ;i!=row;i++)

free(array[i]);

free(array);

return 0;

}

动态创建一维数组

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
int len;

cout<<"输入一维数组大小:"<<endl;

cin>>len;

int *p=new int[len];

cout<<"输入元素,元素之间以空格分隔!"<<endl;

int val,i=0;

for(i=0;i!=len;i++)

{cin>>val;

p[i]=val;

}

cout<<"输出一维数组:"<<endl;

for(i=0;i!=len;i++)

{

cout<<p[i]<<" ";

}

cout<<endl;

动态分配二维数组

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
int main(int argc,char **argv)

{

int column,row;

cout<<"输入二维数组的行数和列数"<<endl;

cin>>row>>column;

int **array;

//array = (int **)malloc(sizeof(int *)*row);//方法一

array=new int *[row];

for(int i=0;i!=row ; i++)

//array[i]=(int *) malloc(sizeof(int )*column);//方法一

array[i]=new int [column];

cout<<"输入二维数组"<<endl;

for(int j=0 ; j !=row ; j++)

{for(int k=0 ; k !=column ; k++) {cin>>array[j][k]; } }

cout<<"输入的二维数组为"<<endl;

for( int j=0 ; j !=row ; j++ )

{ for(int k=0 ; k !=column ; k++)

{cout<<array[j][k]<<" "; }

cout<<endl; }

//释放空间   

for(int i=0 ;i!=row;i++)

free(array[i]);

free(array);

return 0;

}

C++中在结构体里面动态创建数组,而且创建动态结构体数组

大家看一下这个例子就知道了!

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33
int main(int argc, char* argv[])

{

int n,i,m,j;

struct test

{

int *array;

};

test *testarray;

cin>>n>>m;

testarray=new test[m];

for (i=0;i<m;i++)

{

testarray[i].array=new int[n];

}

for (i=0;i<m;i++)

{

for (j=0;j<n;j++)

{

testarray[i].array[j]=i+j;

}

}

for (i=0;i<m;i++)

{

for (j=0;j<n;j++)

{

cout<<testarray[i].array[j];

}

cout<<endl;

}

return 0;

}

总的思想是,先生成结构体数组,再在每个元素里面声明动态数组

就是先实例化,再在实例化的元素里面声明动态数组

可以通过在里面填入东西,进行测试一下!

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 C/C++ 动态数组的创建的实例详解 https://www.kuaiidc.com/72772.html

相关文章

发表评论
暂无评论