Vectors
Explore the use of std::vector in C++, focusing on its dynamic size, memory management, and element access methods. Understand constructors, size versus capacity, and how to modify vectors using functions like assign, emplace, and erase.
We'll cover the following...
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.
🔑
Distinguish the round and curly braces by the creation of astd::vectorIf you construct a
std::vector, you have to keep a few specialities in mind. The constructor with round braces in the following example creates astd::vectorwith 10 elements, the constructor with curly braces astd::vectorwith 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)orstd::vector<int>{10, 2011}. In the first case, you get a ...