Lecture 19 - Implementing Vector

Lecture 19 - Implementing Vector - Implementing...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Implementing vector<> Lecture 26 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/˜hkaiser/fall_2011/csc1254.html
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Vector Vector is the most useful container ▫ Simple ▫ Compactly stores elements of a given type ▫ Efficient access ▫ Expands to hold any number of elements ▫ Optionally range-checked access How is that done? ▫ That is, how is vector implemented? We'll answer that gradually, feature after feature Vector is the default container ▫ prefer vector for storing elements unless there's a good reason not to 12/01/2011, Lectures 26 CSC1254, Fall 2011, Implementing vector<> 2
Background image of page 2
Building from the ground up The hardware provides memory and addresses ▫ Low level ▫ Untyped ▫ Fixed-sized ▫ No checking ▫ As fast as the hardware architects can make it The application builder needs something like a vector ▫ Higher-level operations ▫ Type checked ▫ Size varies (as we get more data) ▫ Run-time checking ▫ Close-to optimally fast 12/01/2011, Lectures 26 CSC1254, Fall 2011, Implementing vector<> 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Building from the ground up At the lowest level, close to the hardware, life’s simple and brutal ▫ You have to program everything yourself ▫ You have no type checking to help you ▫ Run-time errors are found when data is corrupted or the program crashes We want to get to a higher level as quickly as we can ▫ To become productive and reliable ▫ To use a language “fit for humans” We showed all the steps needed ▫ The alternative to understanding is to believe in “magic” ▫ The techniques for building vector are the ones underlying all higher-level work with data structures 12/01/2011, Lectures 26 CSC1254, Fall 2011, Implementing vector<> 4
Background image of page 4
Vector A vector ▫ Can hold an arbitrary number of elements Up to whatever physical memory and the operating system can handle ▫ That number can vary over time E.g. by using push_back() ▫ Example: vector< double > age(4); age[0] = .33; age[1] = 22.0; age[2] = 27.2; age[3] = 54.2; 12/01/2011, Lectures 26 CSC1254, Fall 2011, Implementing vector<> 5 4 0.33 22.0 27.2 54.2 age: age[0]: age[1]: age[2]: age[3]:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Vector // a very simplified vector of doubles (like // vector<double>): class vector { int sz; // the number of elements (“the size”) double * elem; // pointer to the first element public : vector( int s); // constructor: allocate s elements, // let elem point to them // store s in sz int size() const { return sz; } // the current size }; * means “pointer to” so double* is a “pointer to double” What is a “pointer”? How do we make a pointer “point to” elements?
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

Lecture 19 - Implementing Vector - Implementing...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online