1 / 42
文档名称:

数组指针与字符串.ppt

格式:ppt   大小:468KB   页数:42页
下载后只包含 1 个 PPT 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

数组指针与字符串.ppt

上传人:wz_198620 2017/8/6 文件大小:468 KB

下载得到文件列表

数组指针与字符串.ppt

相关文档

文档介绍

文档介绍:第六章数组指针与字符串
数组
指针
动态内存分配
字符串
小结
数组



1. 数组的定义与使用
例6-1:
#include<iostream>
using namespace std;
void main(){
int A[10],B[10],i; //声明:类型名数组名[下标表达式]
int a[2][3]={{1,0,0},{2,3,4}}; //声明时赋初值,按行存储
for(i=0;i<10;i++)
{ A[i]=i*2-1; //使用:数组名[下标表达式]
B[10-i-1]=A[i];
}
for(i=0;i<10;i++) {
cout<<”A[”<<i<<”]=”<<A[i]<<endl;
cout<<” B[”<<i<<”]=”<<B[i]<<endl;}
}

例6-2:数组元素和数组名作为函数参数来进行数据传递和共享。
#include<iostream>
using namespace std;
void RowSum(int A[ ][4],int nrow)//计算A每行元素之和,nrow为行数
{ for(int i=0; i<nrow; i++)
for(int j=1; j<4; j++) A[i][0]+=A[i][j];
}
void main(){
int Table[3][4]={{1,2,3,4},{2,3,4,5},{3,4,5,6}};
for(int i=0;i<3;i++){
for(int j=0;j<4;j++) cout<<Table[i][j]<<““;
cout<<endl; }
RowSum(Table,3); //数组名实参,传递首地址
for(i=0;i<3;i++) cout<<i<<“:”<<Table[i][0]<<endl; }
1 2 3 4
2 3 4 5
3 4 5 6
0:10
1:14
2:18

1)声明: <类名> <数组名>[下标表达式]...
2)访问:数组名[下标].公有成员名
例如: DATE dates[7];
DATE date2[3][5];
3)对象数组赋初值与赋值
DATE dates[3]={DATE(9,22,2010),//注意数组元素的
DATE(9,23,2010), //赋初值方式;
DATE(9,24,2010)};
dates[0]=DATE(9,24,2010); //数组元素赋值
dates[1]=DATE(9,25,2010);
dates[2]=DATE(9,26,2010);
class DATE
{public:DATE(int m,int d,int y);…}
例6-3:对象数组
//
#ifndef _POINT_H
#define _POINT_H
class point{
public:
point();
point(int xx,int yy);//构造函数重载
~point();
void move(int x,int y);
void print ()const;
private: int X,Y;
};
#endif
例6-3:对象数组(续1)
//
#include”"
point::point()
{ X=0;Y=0; cout<<"Default Constructor called."<<endl;}
point::point(int xx,int yy)
{ X=xx; Y=yy; cout<<"Constructor called."<<endl;}
point::~point()
{ cout<<"Destructor called."<<endl;}
void point::move(int x,int y)
{ X=x; Y=y;}
void point::print()
{cout<<"X:"<<X<<" Y:"<<Y<<endl; }
例6-3:对象数组(续2)
#include <iostream>
using namespace std;
#include “"
void main(){
cout<<"Entering main..."<<endl;
point A[3]={point(1,2)};//声明对象数组并赋初值,A[1]和A[2]调用默认构造函数置空
A[1]=point(3,4); //临时对象给数组元素赋值,用毕释放
for(int i=0;i<3;