Using std::vector and std::list

The std::vector is defined in the <vector> header. Here's the simplest usage example:

#include <vector>int main(){  std::vector<int> vec;  vec.push_back(4);  vec.push_back(2);  for (const auto& elem : vec) {    std::cout << elem;  }}

The std::vector grows dynamically. We should consider the growth factor. When declaring a vector, it has some default capacity, which will then grow upon element insertion. Each time the number of elements exceeds the capacity of the vector, it increases its capacity by a given factor (usually, it doubles its capacity). If we know the approximate number of elements that we will need in the vector, we can optimize its use by initially allocating that capacity for the vector using the

Get Expert C++ 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.