您的位置:首页 > 其它

动态分配数组空间

2013-05-11 20:12 369 查看
1.变长一维数组



这里说的变长数组是指在编译时不能确定数组长度,程序在运行时需要动态分配内存空间的数组。实现变长数组最简单的是变长一维数组,你可以这样做:

[cpp]
view plaincopyprint?

//文件名: array01.cpp
#include <iostream>
using namespace std;
int main()
{
int len;
cin> > len;
//用指针p指向new动态分配的长度为len*sizeof(int)的内存空间

int *p=new int[len];
...........
delete[] p;
return 0;
}

[cpp] 
view plaincopyprint?

//文件名:   array02.cpp    
#include <iostream>    
#include <vector>  
  
using   namespace   std;   
int   main()   
{   
    int   len;   
    cin> > len;   
    vector <int>   array(len);//声明变长数组 
  
    for(int   i=0;i <len;i++)   
    {   
        array[i]=i;   
        cout < <array[i] < < "/t ";   
    }   
    return   0;   
}  

//文件名:   array02.cpp 
#include <iostream> 
#include <vector> 
using   namespace   std; 
int   main() 
{ 
	int   len; 
	cin> > len; 
	vector <int>   array(len);//声明变长数组 
	for(int   i=0;i <len;i++) 
	{ 
		array[i]=i; 
		cout < <array[i] < < "/t "; 
	} 
	return   0; 
}




这里的变长数组让我联想到了java的java.util包中的vector和C#中的ArrayList,它们也可以在各自的语言中实现变长数组。不过C++中的vector不能像C#一样有托管的垃圾回收机制回收被占用的内存空间,但是你可以在使用完vector后调用~vector()析构函数释放内存。







2.变长n维数组

变长的n维数组实现起来有些麻烦,但是在工程与软件设计应用中常使用的是二维数组,所以在这里着重介绍变长的二维数组,变长的n维数组可以按照类似的方法实现。首先看一个经典的用C实现变长二维数组的例子:





[cpp]
view plaincopyprint?

//文件名: array03.c
#include <stdio.h>
#include <malloc.h>


void main()

{
int x,y,i,j;
float **a,*b;
printf( "请输入你所求解的线性方程组的行数x:x= ");
scanf( "%d ",&x);
printf( "请输入你所求解的线性方程组的列数y:y= ");
scanf( "%d ",&y);
a=(float **)malloc(sizeof(float *) *x);
b=(float *)malloc(sizeof(float) *x);
for(i=0;i <x;i++)
{
*(a+i)=(float *)malloc(sizeof(float) *y);
}
/*读入数据*/

printf( "请按行的顺序依次输入系数的值(共%d项): ",x*y);
for(i=0;i <=x-1;i++)
for(j=0;j <=y-1;j++)
scanf( "%f ",&a[i][j]);
printf( "请按列的顺序依次输入常数的值(共%d项): ",x);
for(j=0;j <=x-1;j++)
scanf( "%f ",&b[j]);
printf( "您输入方程组的增广矩阵为:/n ");
for(i=0;i <=x-1;i++)
{
for(j=0;j <=y-1;j++)
printf( "%.5f",a[i][j]);
printf( "%.5f",b[i]);
printf( "/n ");
}
free(b);
for(i=0;i <x;i++)
free (*(a+i));

}

[c-sharp] 
view plaincopyprint?

//文件名:   array04.cpp   
#include   <iostream>   
#include   <iomanip>    
using   namespace   std;   
int   main()   
{   
    int   num1,//行数 
  
    num2;//列数    
    cout < < "Please   enter   the   number   for   row   and   column:   " < <endl;   
    cin   > >   num1   > >   num2;   
    //为二维数组开辟空间    
    int   **p   =   new   int*[num1];   
    for(int   i=0;   i <num1;   ++i)   
        p[i]   =   new   int[num2];   
    for(int   j=0;j <num1;j++)   
    {   
        for(int   k=0;k <num2;k++)   
        {   
            p[j][k]=(j+1)*(k+1);   
            cout < <setw(6) < <p[j][k] < < ': ' < <setw(8) < <&p[j][k];   
        }   
        cout < <endl;   
    }   
    //释放二维数组占用的空间    
    for(int   m=0;m <num1;m++)   
        delete[]   p[m];   
    delete[]   p;   
    return   0;   
}   

//文件名:   array04.cpp 
#include   <iostream> 
#include   <iomanip> 
using   namespace   std; 
int   main() 
{ 
	int   num1,//行数 
	num2;//列数 
	cout < < "Please   enter   the   number   for   row   and   column:   " < <endl; 
	cin   > >   num1   > >   num2; 
	//为二维数组开辟空间 
	int   **p   =   new   int*[num1]; 
	for(int   i=0;   i <num1;   ++i) 
		p[i]   =   new   int[num2]; 
	for(int   j=0;j <num1;j++) 
	{ 
		for(int   k=0;k <num2;k++) 
		{ 
			p[j][k]=(j+1)*(k+1); 
			cout < <setw(6) < <p[j][k] < < ': ' < <setw(8) < <&p[j][k]; 
		} 
		cout < <endl; 
	} 
	//释放二维数组占用的空间 
	for(int   m=0;m <num1;m++) 
		delete[]   p[m]; 
	delete[]   p; 
	return   0; 
}


以下是运行结果:
Please enter the number for row and column:
4 5
1:004915F0 2:004915F4 3:004915F8 4:004915FC 5:00491600
2:00491180 4:00491184 6:00491188 8:0049118C 10:00491190
3:00491140 6:00491144 9:00491148 12:0049114C 15:00491150
4:00491100 8:00491104 12:00491108 16:0049110C 20:00491110
Press any key to continue

程序清单array04.cpp可以显示分配的内存空间单元的地址,大家可以看到,由于数组空间是动态分配的,数组行之间的地址空间是不连续的,因为不同行的数组元素的地址空间是用不同的new来分配的。而每一行之中列之间的地址空间是连续的。

那么用vector(向量)怎样实现二维数组呢?以下给出源程序:

[cpp]
view plaincopyprint?

//文件名: array05.cpp
#include <iostream>
#include <vector>

#include <iomanip>
using namespace std;
int main()
{
int i,
j,
m, //行数
n; //列数
cout < < "input value for m,n: ";
cin> > m> > n;
//注意下面这一行:vector <int后两个 "> "之间要有空格!否则会被认为是重载 "> > "。

vector <vector <int> > vecInt(m, vector <int> (n));
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
vecInt[i][j] = i*j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
cout < <setw(5) < <vecInt[i][j] < < ": " < <setw(9) < <&vecInt[i][j];
cout < <endl;
}
return 0;
}

[cpp] 
view plaincopyprint?

//文件名:   array06.cpp    
#include   <iostream>    
#include   <vector>  
  
#include   <iomanip>    
using   namespace   std;   
int   main()   
{   
    int   i,   
    j,   
    k,   
    m,   //一维坐标    
    n,   //二维坐标    
    l;   //三维坐标    
    cout   < <   "input   value   for   m,n,l: ";   
    cin> > m> > n> > l;   
    vector <vector <vector <int>   >   >   vecInt(m,   vector <vector <int>   > (n,   vector <int> (l)));       
    for   (i   =   0;   i   <   m;   i++)   
        for   (j   =   0;   j   <   n;   j++)   
            for(k   =   0;   k   <   l;   k++)   
                vecInt[i][j][k]   =   i+j+k;     
    for   (i   =   0;   i   <   m;   i++)   
    {   
        for   (j   =   0;   j   <   n;   j++)   
        {   
            for(k   =   0;   k <l;   k++)   
                cout < <setw(5) < <vecInt[i][j][k] < < ": " < <setw(9) < <&vecInt[i][j][k];   
            cout < <endl;   
        }   
        cout < <endl;   
    }   
    return   0;   
}   

//文件名:   array06.cpp 
#include   <iostream> 
#include   <vector> 
#include   <iomanip> 
using   namespace   std; 
int   main() 
{ 
	int   i, 
	j, 
	k, 
	m,   //一维坐标 
	n,   //二维坐标 
	l;   //三维坐标 
	cout   < <   "input   value   for   m,n,l: "; 
	cin> > m> > n> > l; 
	vector <vector <vector <int>   >   >   vecInt(m,   vector <vector <int>   > (n,   vector <int> (l)));     
	for   (i   =   0;   i   <   m;   i++) 
		for   (j   =   0;   j   <   n;   j++) 
			for(k   =   0;   k   <   l;   k++) 
				vecInt[i][j][k]   =   i+j+k;   
	for   (i   =   0;   i   <   m;   i++) 
	{ 
		for   (j   =   0;   j   <   n;   j++) 
		{ 
			for(k   =   0;   k <l;   k++) 
				cout < <setw(5) < <vecInt[i][j][k] < < ": " < <setw(9) < <&vecInt[i][j][k]; 
			cout < <endl; 
		} 
		cout < <endl; 
	} 
	return   0; 
}


运行结果:
input value for m,n,l:2 3 4
0: 00492FE0 1: 00492FE4 2: 00492FE8 3: 00492FEC
1: 00492FA0 2: 00492FA4 3: 00492FA8 4: 00492FAC
2: 00492F60 3: 00492F64 4: 00492F68 5: 00492F6C

1: 00492EC0 2: 00492EC4 3: 00492EC8 4: 00492ECC
2: 00492E80 3: 00492E84 4: 00492E88 5: 00492E8C
3: 00492E40 4: 00492E44 5: 00492E48 6: 00492E4C
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: