Loading...
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
A std::vector is a dynamic array that can grow and shrink in size automatically. It's the most used container in modern C++.
#include <vector>
vector<int> v = {1, 2, 3};
v.push_back(4); // Adds 4 to end
v.pop_back(); // Removes last element
cout << v.size(); // 3
cout << v[0]; // Access like arrayElements are stored in contiguous memory, ensuring O(1) random access.
Inserting at the end (push_back) is very efficient on average.
Size is the current number of elements. Capacity is the total space allocated. Vectors often allocate more memory than they need to avoid constant reallocations.