当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

《数据结构的算法在C++中的应用》(英文版)Chapter 4 Arrays and Matrix

资源类别:文库,文档格式:PPT,文档页数:58,文件大小:181KB,团购合买
4.1 1D-Array 1. One-dimensional array 1D-array is a limited sequence composed of n (n20) elements which are of the same data type. For example:
点击下载完整版文档(PPT)

Chapter 4 arrays and matrix

Chapter 4 Arrays and Matrix

4.1 1D-Array 1. One-dimensional array 1D-array is a limited sequence composed ofn(n20) elements which are of the same data type For example 0123456789 a3527491860|5477|834102 Location of the element Loc(all=loc(aod+

4.1 1D-Array 1. One-dimensional array • 1D-array is a limited sequence composed of n (n0) elements which are of the same data type. • For example: • Location of the element Loc(a[i])=Loc(a[0])+i 35 27 49 18 60 54 77 83 41 02 a 0 1 2 3 4 5 6 7 8 9

4.1 ID-Array Class definition of lD-Array(program 4-1) template class arraylDi public Array ld(int size=0) Array l D(const Array ld& v) Array ldoidelete d element; T& operator l(int const int Sizeoireturn size; some operation

4.1 1D-Array Class definition of 1D-Array(program 4-1) template class Array1D{ public: Array1D(int size=0); Array1D(const Array1D& v); ~Array1D(){delete [] element;} T& operator[](int i)const; int Size(){return size;} //some operation

4.1 1D-Array rivate int size T*element; //Id array

4.1 1D-Array private: int size; T*element; //1D array };

4.1 1D-Array 1)Constructor for ID array Program 4-2 template ArrayID: Arrayld(int sz) f if(sz<o) throw badInitializerso; SIze-SZ element=new t[]

4.1 1D-Array 1)Constructor for 1D array Program 4-2 template Array1D::Array1D(int sz) { if(sz<0) throw BadInitializers(); size=sz; element=new T[sz]; }

4.1 1D-Array Template. Array lD(const Array lD& v) f//copy constructor for ID array Size-v size element-=new T[size]; /get space for(int i=0; i<size; i++)//copy elements elementi=v element[i]

4.1 1D-Array Template Array1D::Array1D(const Array1D& v) {//copy constructor for 1D array size=v.size; element=new T[size]; //get space for(int i=0;i<size;i++) //copy elements element[i]=v.element[i]; }

4.1 1D-Array Overloading the array indexing operator Program 4.3 template T& arrayld:: operator[(int iconst f//return reference to element I if(i=size )throw OutofBoundso return element[1

4.1 1D-Array 2)overloading the array indexing operator Program 4.3 template T& Array1D::operator[](int i)const {//return reference to element I. if(i=size)throw OutOfBounds(); return element[i]; }

4.1 1D-Array 3)Overloading the assignment operator Program 4.4 template Array id& array ld: operator- (const Arrayld& v fif (thisl=&v)//not self-assignment SIze-Vsize delete d element; //free old space element-new T[size]; //get right amout

4.1 1D-Array 3)Overloading the assignment operator Program 4.4 template Array1D& Array1D::operator= (const Array1D& v) {if (this!=&v){//not self-assignment size=v.size; delete [] element; //free old space element=new T[size];//get right amout

4.1 1D-Array for(int 1=0; K<size; 1++)/copy elements elementi=v elementi return *this

4.1 1D-Array for(int i=0;i<size;i++) //copy elements element[i]=v.element[i]; } return *this; }

4.2 2D-Array Two-dimensional arrays are composed of n rows and m columns doo aoi a02… 10a11 2 m Am=|aa21a2…l2 10

4.2 2D-Array Two-dimensional arrays are composed of n rows and m columns. a00 a01 a02……a0 m-1 a10 a11 a12……a1 m-1 a20 a21 a22……a2 m-1 …………. an-10 an-11an-12…..an-1 m-1 A[n][m]=

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共58页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有