下载此文档

数组指针与字符串.pptx


文档分类:IT计算机 | 页数:约41页 举报非法文档有奖
1/41
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/41 下载此文档
文档列表 文档介绍
数组



库文档分享
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;i++){
A

数组指针与字符串 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数41
  • 收藏数0 收藏
  • 顶次数0
  • 上传人wz_198614
  • 文件大小193 KB
  • 时间2018-10-21