Course Data Structures Essentials

Dynamic Vector

Course's Page 15 min Text by
User Image
Diego Rangel

Hello again ๐Ÿ˜ƒ. In this class you will learn about Dynamic Vector and its main functions.

  • Introduction to Dynamic Vector
  • Dynamic Vector in C++
  • Dynamic Vector in Python

Introduction

Imagine a situation where we need to use a vector-type structure, but we don't know the size of the input or whether new elements will be inserted later during the execution of a program. For this type of situation, there is a data structure that implements a vector, but it is possible to expand its size according to the programmer's needs.

The Dynamic Vector is a variable size data structure that starts with a constant size . When its capacity is exceeded, the Dynamic Vector increases the size of the structure by (Expansion Factor). In practice, several implementations simply double the capacity of the Dynamic Vector when it is exceeded. Unlike the List, in the Dynamic Vector, we can access any element in time via the direct access operator, because a Dynamic Vector always tries to store the elements in memory sequentially, just like a conventional array. Figure 1 shows the behavior of the Dynamic Vector when its capacity is exceeded and how it doubles its capacity.

Figure 1 : Insertion of element 5 at the end of the dynamic vector

Note that even having its size is 5, because the vector has only 5 elements stored, being able to store 3 more elements before its capacity doubles again.

std::vector

The std::vector is the implementation of the Dynamic Vector in C++. In the C++ implementation whenever the capacity is exceeded the vector doubles in size .