面向对象技术(CPrimer)第4章.ppt

上传人:牧羊曲112 文档编号:6613930 上传时间:2023-11-18 格式:PPT 页数:38 大小:497.50KB
返回 下载 相关 举报
面向对象技术(CPrimer)第4章.ppt_第1页
第1页 / 共38页
面向对象技术(CPrimer)第4章.ppt_第2页
第2页 / 共38页
面向对象技术(CPrimer)第4章.ppt_第3页
第3页 / 共38页
面向对象技术(CPrimer)第4章.ppt_第4页
第4页 / 共38页
面向对象技术(CPrimer)第4章.ppt_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《面向对象技术(CPrimer)第4章.ppt》由会员分享,可在线阅读,更多相关《面向对象技术(CPrimer)第4章.ppt(38页珍藏版)》请在三一办公上搜索。

1、第四章 数组和指针 1,数组与vector类型相似,也可以保存某种类型的一组对象。区别在于数组的长度是固定的。数组一经创建,就不允许添加元素。程序员无法知道一个给定数组的长度。指针可以像迭代器一样用于遍历和检查数组中的元素。使用指针很容易出错。与容器和迭代器相比,依赖于数组和指针的程序更容易出错。现代C+程序更多使用vector代替数组。数组被严格限制使用,只有当测试性能表明用vector达不到性能要求时,才使用数组。,4.1 数组的定义和初始化 2,数组是由类型名、标识符和维数组成的复合数据类型。类型名规定了存放在数组中的元素的类型维数指定了数组中包含的元素的个数。必须用值大于等于1的常量表

2、达式定义。const unsigned buf_size=512,max_files=20;int staff_size=27;/conconstconst unsigned sz=get_size();/const value not known/untile run timechar input_bufferbuf_size;/ok,const variablestring fileTalbemax_files+1;/ok,constant expressiondouble salariesstaff_size;/error,non const variableint test_score

3、sget_size();/error;non const expressionint test_scoresget_size();/error;non const expressionint valssz;/error;size not known untiol run time,数组初始化 3,1.显式初始化数组元素 const unsigned arr_size=3;int iaarray_size=0,1,2 若无显式初始化,则:在函数体外定义的内置数组,无素均为0.函数体内定义的内置数组,元素无初始化若元素为类类型,无论在那里定义,则自动调用该类的默认构造函数进行初始化。如果该类没有默

4、认构造函数,则必须为该数组的元素提供显式初始化。,4,Int ia=0,1,2;/an array of dimension 3Const unsigned array_size=5;Int iaarray_size=0,1,2;/ia=0,1,2,0,0String str_arrarr_size=“hi”,”bye”;/str_arr=“hi”,”bye”,”,”,”,特殊的字符数组 5,char ca1=C,+,+;/no nullChar ca2=C,+,+,0;/explicit nullChar ca3=“C+”;/null added automaticlConst char c

5、a36=“Daniel”;/error,is/7 elements,数组不能直接复制和赋值 6,与vector不同,一个数组不能用另外一个数组初始化,也不能将一个数组赋值给另一个数组。Int ia=0,1,2;/ok,array of intsInt ia2(ia);/error,cannot initialize any/array with anotherInt main()const unsigned array_size=3;int ia3array_size;/ok,but ele are unintialize ia3=ia;/error,cannot assign one ara

6、y to another return 0;,数组操作 7,数组元素可用下标操作符访问。从0开始。Vector的下标类型为vector:size_type,数组的下标类型为size_tInt main()const size_t array_size=10;int iaarray_size;/10 ints for(size_t ix=0;ix!=array_size;+ix)iaix=ix;return 0;,8,Int main()const size_t array_size=7;int ia1=0,1,2,3,4,5,6;int ia2array_size;/local,el unin

7、itial/copy element form ia1 to ia2 for(size_t ix=0;ix!=array_size;+ix)ia2ix=ia1ix;return 0;,4.2 指针的引入 9,指针是指向某种类型对象的复合数据类型,是用于数组的迭代器:指向数组中的一个元素。可使用解引用操作符*和自增操作符。指针用于指向对象。指针保存的是另一个对象的地址:string s(“hello world”);string*sp=/sp holds the addres of s,建议:尽量避免使用指针和数组 10,由于指针和数组容易产生不可预料的错误,其中一部分是概念上的问题,指针用于低

8、纵级操作,容易产生与繁琐细节相关的错误。其他错误则源于使用指针的语法规则,特别是声明指针的语法。许多有用的程序都可不使用指针和数组实现。现代C+程序采用vector类和迭代器取代一般数组、采用string类取代C风格字符串。,指针的定义和初始化 11,vector*pvec;/pvec can point to a/vectorint*ip1,*ip2;/ip1 and ip2 can point to intstring*pstring;/pstring can point to a stridouble*dp;/dp can point to a douledouble dp,*dp2;/

9、dp2 is a pointer另一种风格的指针:string*ps;/legal but can be misleadingString*ps1,ps2;/ps1 is pointer to str,ps2 is stiString*ps1,*ps2;/both ps1 and ps2 are pointer,指针可能的取值 12,指针的状态:保存一个特定对象的地址;指向某个对象后面的另一个对象;或者是0值,表示不指向任何对象。int ival=1024;int*pi=0;/pi initialized to address no objint*pi2=/pi2 not address to

10、 no object,指针初始化和赋值操作的约束 13,对指针进行初始化或赋值只能用以下四种1.0值常量表达式。如在编译时可获得0值的整形const对象或字面值常量0.2.类型匹配的对象的地址。3.另一个对象之后的下一地址。4.同类型的另一个有效指针。,14,int ival;int zero=0;const int c_ival=0;int*pi=ival;/error,initial from int valuePi=zero;/error,pi assigned int value of 0Pi=c_ival;/ok,c_ival is a const with comple-time

11、value of 0pi=0;/ok,directly with liteal constant 0Int*pi=NULL;Double dval;Double*pd=/error,void*指针 15,void*指针可以保存任何类型的对象地址。double obj=3.14;double*pd=/pd can be a pointer to any typeVoid*指针支持的操作:与另一指针比较向函数传递void*指针或从函数返回void*指针给另一个void*指针赋值,4.2.3 指针操作 16,指针提供间接操纵其所指对象的功能。stirng s(“hello world”);strin

12、g*sp=/prints hello world,给指针赋值或通过指针赋值 17,如果对左操作数进行解引用,则修改的是指针所指对象的值。如果没有使用解引用,则修改的是指针本身的值。string s1(“some value”);string*sp1=/sp1point to a different ofjbect,指针和引用的比较 18,引用总是指向某个对象,定义引用时没有初始化是错误的。指针可以指向不同的对象。赋值行为的差异:给引用赋值修改的是该引用所关联的对象的值。指针赋值使指针指向另一个对象。,19,int ival=1024,ival2=2048;int*pi=/asigns ival

13、2 to ival,指向指针的指针 20,int ival=1024;int*pi=,使用指针访问数组元素 21,指针是数组的迭代器。Int ia=0,2,4,6,8;Int*ip=ia;/iip points to ia0Ip=/ok,ip2 point to ia4,22,ptrdiff_t n=ip2-ip;/ok,distan between themint last=*(ia+4);/equal to ia4int last=*ia+4;/ok,last=4int ia=0,2,4,6,8int i=ia0;/ia point to the first elementint*p=/o

14、k,p-2=ia0,数组的超出末端指针 23,const size_t arr_size=5;int arrarr_size=1,2,3,4,5;int*p=arr;/ok,p point to arr0int*p2=p+arr_size;/p2 point to one past/the end,用指针遍历数组元素 24,Const size_t arr_sz=5;Int int_arrarr_sz=0,1,2,3,4;for(int*pbegin=int_arr,*pend=int_arr+arr(sz;pbegin!=pend;+pbegin)cout*pbegin;,指向const对象

15、的指针和const指针25,指向const对象的指针必须具有const特性const double*cptr;/cptr point to a/double that is constConst double pi=3.14;Cptr=/ok,but cant change dval through cptr,const指针 26,const指针是指本身的值不能被修改的指针int errNumb=0;int*const curErr=,指针和typedef 27,typedef string*pstringconst pstring cstr;问:cstr是什么类型?const string*

16、cstr;/errorstring*const cstr;/correct!,4.3 C风格字符串 28,字符串字面值类型其实是const char类型的数组。而是以空字符null结束的字符数组。char ca1=C,+,+;/not null,not c stylchar ca2=C,+,+,0;/explicit nullchar ca3=“C+”;/null added automaticchar*cp1=ca1;char*cp2=ca2;,C风格字符串的标准库函数 29,#include 标准库函数总是假定每个字符串是以null结束的。,4.3.1 创建动态数组 30,数组长度固定,在

17、编译时必须知道其长度,只在定义它的块语句内存在。动态分配的数组不必在编译时知道其长度,可以在运行时才确定数组长度。动态分配的数组将一直存在,直到程序显式释放它为止。C语言用一对标准库函数malloc和free在自由存储区中分配存储空间,C+用new和delete实现相同的功能。,动态数组的定义 31,动态分配数组时,只需指定类型和长度,不必为数组对象命名。int*pia=new int10;/array of 10 ints,初始化动态分配的数组 32,string*psa=new string10;/arr of 10 emint*pia=new int10;/arr of 10 unini

18、ti intsInt*pia2=new int10();,const对象的动态数组 33,/error,unitilized const arrayConst int*pci_bad=new const int100;/ok,value-initialized const arrayconst int*pci_ok=new const int100();/ok,array of 100 empty stringsConst string*pcs=new const string100;,允许动态分配空数组 34,size_t n=get_size();int*p=new intn;for(in

19、t*q=p;q!=p+n;+q)cout*qendl;Char arr0;/error,cant define 0-lenth arChar*cp=new char0;/ok,but cp cant/be dereferenced,动态空间的释放 35,如果不再需要使用动态创建的数组,程序员必须显式地将其占用的存储空间返还给程序的自由存储区。C+为指针提供delete释放指针所指向的数组空间。char*cp=new char10;Delete cp;,4.4 多维数组 36,如果数组的元素又是数组,称为多维数组int ia34=0,1,2,3,4,5,6,7,8,9,10,11;int ia34=0,1,2,3,4,5,6,7,8,9,10,11;Int ia34=0,4,8;,指针和多维数组 37,由于多维数组其实是数组的数组,由多维数组转换成的指针类型应是指向第一个内层数组的指针。int ia34;int(*ip)4=ia;/ip points to an array of/4intsip=/array of pointers to int,作业,4.5,4.7,4.16,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号