Implementing the Vector Class

Steven J. Zeil

Old Dominion University, Dept. of Computer Science

Table of Contents

1. Required Performance
2. Implementing a vector class
2.1. Focus on push_back
2.2. Adding to an Array That Has Unused Space
2.3. Adding to an Array That is Full
2.4. Expanding the Array
2.5. Copying the Data into the Explanded Array
2.6. Discarding the Old Array
2.7. Adding to an Array That Has Unused Space (Reprise)
3. Coding push_back
3.1. Are we Full?
3.2. If Full, Reserve More Space
3.3. push_back: add to an array that has space
4. Performance
4.1. Looking at push_back
4.2. Using reserve() to get a True O(1) Worst Case
4.3. Summary

A vector is similar to an array, except that it can "grow" to accommodate as many items as you actually try to put into it:

In the Forum:

(no threads at this time)