Arrays and Vectors
A plain array has a fixed size. vector (from <vector>) is a resizable array: push_back, pop_back, size, at, front, back, clear and empty. Prefer vector in modern code. sort from <algorithm> sorts a range.
Example
vector<int> nums = {5, 2, 9, 1};
nums.push_back(7);
sort(nums.begin(), nums.end());
for (int n : nums) cout << n << " ";
cout << endl;
cout << nums.size() << " " << nums.front() << " " << nums.back() << endl;
int fixed[3] = {1, 2, 3};
cout << fixed[1] << endl;Result
1 2 5 7 9 5 1 9 2
Exercise
Add an item to the end.
nums.(7);
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.