std::vector

A vector, which is like an array, is a container to store a bunch of items contiguously. However, the vector can double its size automatically if we insert a new item when its capacity has been reached. Also, vectors have many member functions that arrays don't have, and provide iterators that act like pointers but aren't.

Don't forget to include the vector header at the beginning of code file if we are going to use the std::vector data type. To create a new vector object, we can initialize it as follows:

// Initialize a vectorvector<int> vectorlist = { 35, 41, 94 };

The preceding code will create a new vector containing three elements. However, we can initialize an empty vector and insert the vector using the push_back() method ...

Get C++ Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.