Using the Dynamically Allocated Vector Class
The Standard Library also provides the vector class in the <vector> header. Again, this class is a template, so you can use it with built-in and custom types. However, unlike array, the memory is dynamically allocated, which means that a vector can be expanded or shrunk at runtime. The items are stored contiguously so you can access the underlying buffer by calling the data function or accessing the address of the first item (to support resizing the collection, the buffer may change, so such pointers should only be used temporarily). And, of course, as with array, there is no automatic conversion to a dumb pointer. The vector class provides indexed random access with square bracket syntax and a range check with the at function. The class also implements the methods to allow the container to be used with Standard Library functions and with ranged for.
The vector class has more flexibility than the array class because you can insert items, and move items around, but this does come with some overhead. Because instances of the class allocate memory dynamically at runtime there is a cost of using an allocator and there is some extra overhead in initialization and destruction (when the vector object goes out of scope). Objects of the vector class also take more memory than the data it holds. For this reason, it is not suitable for small numbers of items (when array is a better choice).