Chapter 19Vectors, templates, and exceptions.ppt
《Chapter 19Vectors, templates, and exceptions.ppt》由会员分享,可在线阅读,更多相关《Chapter 19Vectors, templates, and exceptions.ppt(43页珍藏版)》请在麦多课文档分享上搜索。
1、Chapter 19 Vectors, templates, and exceptions,Bjarne S is the third of the lectures exploring the design of the standard library vector and the techniques and language features used to implement it. Here, we deal with changing the size of a vector, parameterization of a vector with an element type (
2、templates), and range checking (exceptions).,2,Stroustrup/Programming,Overview,Vector revisited How are they implemented? Pointers and free store Destructors Initialization Copy and move Arrays Array and pointer problems Changing size resize() and push_back() Templates Range checking and exceptions,
3、3,Stroustrup/Programming,Changing vector size,Fundamental problem addressed We (humans) want abstractions that can change size (e.g., a vector where we can change the number of elements). However, in computer memory everything must have a fixed size, so how do we create the illusion of change? Given
4、 vector v(n); / v.size()=nwe can change its size in three ways Resize it v.resize(10); / v now has 10 elementsAdd an element v.push_back(7); / add an element with the value 7 to the end of v / v.size() increases by 1 Assign to it v = v2; / v is now a copy of v2 / v.size() now equals v2.size(),4,Stro
5、ustrup/Programming,Representing vector,If you resize() or push_back() once, youll probably do it again; lets prepare for that by sometimes keeping a bit of free space for future expansionclass vector int sz;double* elem;int space; / number of elements plus “free space”/ (the number of “slots” for ne
6、w elements) public:/ ;,5,allocation:,sz:,-elements- (initialized),-free space- (uninitialized),0,Stroustrup/Programming,Representing vector,An empty vector (no free store use):A vector(n) (no free space):,6,N:,0,Stroustrup/Programming,vector:reserve(),First deal with space (allocation); given space
7、all else is easy Note: reserve() doesnt mess with size or element valuesvoid vector:reserve(int newalloc)/ make the vector have space for newalloc elements if (newalloc=space) return; / never decrease allocationdouble* p = new doublenewalloc; / allocate new spacefor (int i=0; isz; +i) pi=elemi; / co
8、py old elementsdelete elem; / deallocate old spaceelem = p; space = newalloc; ,7,Stroustrup/Programming,vector:resize(),Given reserve(), resize() is easy reserve() deals with space/allocation resize() deals with element valuesvoid vector:resize(int newsize)/ make the vector have newsize elements/ in
9、itialize each new element with the default value 0.0 reserve(newsize); / make sure we have sufficient spacefor(int i = sz; inewsize; +i) elemi = 0; / initialize new elementssz = newsize; ,8,Stroustrup/Programming,vector:push_back(),Given reserve(), push_back() is easy reserve() deals with space/allo
10、cation push_back() just adds a valuevoid vector:push_back(double d)/ increase vector size by one/ initialize the new element with d if (sz=0) / no space: grab somereserve(8);else if (sz=space) / no more free space: get more spacereserve(2*space);elemsz = d; / add d at end+sz; / and increase the size
11、 (sz is the number of elements) ,9,Stroustrup/Programming,resize() and push_back(),class vector / an almost real vector of doublesint sz; / the sizedouble* elem; / a pointer to the elementsint space; / size+free_space public:/ constructors and destructors double,10,Stroustrup/Programming,The this po
12、inter,A vector is an object vector v(10); vector* p = / we can point to a vector object Sometimes, vectors member functions need to refer to that object The name of that “pointer to self” in a member function is this,11,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,10,v:,this:,10,p:,Stroustrup/Programming
13、,The this pointer,vector / rare use made possible by operator=() returning *this/ The this pointer has uses that are less obscure one of which well get to in two minutes,12,Stroustrup/Programming,Assignment,Copy and swap is a powerful general ideavector / return a self-reference ,13,Stroustrup/Progr
14、amming,Optimize assignment,“Copy and swap” is the most general idea but not always the most efficient What if there already is sufficient space in the target vector? Then just copy! For example: a = b;,14,sz:,sz:,b:,a:,Stroustrup/Programming,Optimized assignment,vector ,15,Stroustrup/Programming,Tem
15、plates,But we dont just want vector of double We want vectors with element types we specify vector vector vector vector / vector of pointers vector / vector of vectors vector We must make the element type a parameter to vector vector must be able to take both built-in types and user-defined types as
16、 element types This is not some magic reserved for the compiler; we can define our own parameterized types, called “templates”,16,Stroustrup/Programming,Templates,The basis for generic programming in C+ Sometimes called “parametric polymorphism” Parameterization of types (and functions) by types (an
17、d integers) Unsurpassed flexibility and performance Used where performance is essential (e.g., hard real time and numerics) Used where flexibility is essential (e.g., the C+ standard library)Template definitions template class Buffer /* */ ; template void fill(Buffer thats what buf has,17,Stroustrup
18、/Programming,Parameterize with element type,/ an almost real vector of Ts: template class vector / ; vector vd; / T is double vector vi; / T is int vector vvi; / T is vector/ in which T is int vector vc; / T is char vector vpd; / T is double* vector* vvpd; / T is vector*/ in which T is double,18,Str
19、oustrup/Programming,Basically, vector is,/ an almost real vector of doubles: class vector int sz; / the sizedouble* elem; / a pointer to the elementsint space; / size+free_space public:vector() : sz(0), elem(0), space(0) / default constructorexplicit vector(int s) :sz(s), elem(new doubles), space(s)
20、 / constructorvector(const vector,19,Stroustrup/Programming,Basically, vector is,/ an almost real vector of chars: class vector int sz; / the sizechar* elem; / a pointer to the elementsint space; / size+free_space public:vector() : sz0, elem0, space0 / default constructorexplicit vector(int s) :szs,
21、 elemnew chars, spaces / constructorvector(const vector,20,Stroustrup/Programming,Basically, vector is,/ an almost real vector of Ts: template class vector / read “for all types T” (just like in math)int sz; / the sizeT* elem; / a pointer to the elementsint space; / size+free_space public:vector() :
22、 sz0, elem0, space0; / default constructorexplicit vector(int s) :szs, elemnew Ts, spaces / constructorvector(const vector,21,Stroustrup/Programming,Basically, vector is,/ an almost real vector of Ts: template class vector / read “for all types T” (just like in math)int sz; / the sizeT* elem; / a po
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER19VECTORS TEMPLATES ANDEXCEPTIONSPPT
