Vectors

A more refined version of arrays, vectors simplify insertion and deletion of values.

We'll cover the following...
widget

std::vector is a homogeneous container, for which its length can be adjusted at runtime. std::vector needs the header <vector>. As it stores its elements contiguously in memory, std::vector supports pointer arithmetic.

Press + to interact
for (int i= 0; i < vec.size(); ++i){
std::cout << vec[i] == *(vec + i) << std::endl; // true
}

🔑
Distinguish the round and curly braces by the creation of a std::vector

If you construct a std::vector, you have to keep a few specialities in mind. The constructor with round braces in the following example creates a std::vector with 10 elements, the constructor with curly braces a std::vector with the element 10.

std::vector<int> vec(10);
std::vector<int> vec{10};

The same rules hold for the expressions std::vector<int>(10, 2011) or std::vector<int>{10, 2011}. In the first case, you get ...