tailieunhanh - Lecture Programming principles and practice using C++: Chapter 19 - Bjarne Stroustrup

This 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 (templates), and range checking (exceptions). | Chapter 19 Vectors, templates, and exceptions Bjarne Stroustrup Abstract This 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 (templates), and range checking (exceptions). 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 Stroustrup/Programming Changing vector size Fundamental problem addressed We (humans) want abstractions that can change size (., 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 vector v(n); // ()==n we can . | Chapter 19 Vectors, templates, and exceptions Bjarne Stroustrup Abstract This 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 (templates), and range checking (exceptions). 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 Stroustrup/Programming Changing vector size Fundamental problem addressed We (humans) want abstractions that can change size (., 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 vector v(n); // ()==n we can change its size in three ways Resize it (10); // v now has 10 elements Add an element (7); // add an element with the value 7 to the end of v // () increases by 1 Assign to it v = v2; // v is now a copy of v2 // () now equals () Stroustrup/Programming Representing vector If you resize() or push_back() once, you’ll probably do it again; let’s prepare for that by sometimes keeping a bit of free space for future expansion class vector { int sz; double* elem; int space; // number of elements plus “free space” // (the number of “slots” for new elements) public: // }; 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): N: 0 Stroustrup/Programming vector::reserve() First deal with space (allocation); given space all else is easy Note: reserve() doesn’t mess with size or element