Lesson 1 of 46
Lesson Progress: 0%
The vector is a dynamic array from the C++ Standard Library. It can grow and shrink at runtime while providing contiguous storage like a regular array. Vectors are declared with #include <vector> and are one of the most commonly used containers. This lesson covers creating vectors, adding elements with push_back, accessing items with brackets and at(), iterating with loops, modifying elements, and removing the last element. Understanding vectors is essential for writing flexible and efficient C++ programs.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums;
nums.push_back(10);
nums.push_back(20);
nums.push_back(30);
cout << nums.size();
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
vector<int> declares a vector that holds integers.push_back() appends a new element to the end of the vector.size() returns how many elements are currently stored.#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {10, 20, 30};
cout << nums[0] << " ";
cout << nums.at(2);
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
vector[index] or the safer vector.at(index).at() method performs bounds checking and throws an exception if the index is invalid.#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {5, 10, 15};
for (int i = 0; i < nums.size(); i++) {
if (i > 0) cout << " ";
cout << nums[i];
}
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
for loop with size() iterates over all elements by index.i < nums.size() ensures you stay within bounds.if (i > 0) check.#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {1, 2, 3};
nums[1] = 99;
for (int i = 0; i < nums.size(); i++) {
if (i > 0) cout << " ";
cout << nums[i];
}
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
nums[1] = 99 changes the second element without changing the size.#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {1, 2, 3};
cout << nums.size() << " " << nums[1];
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
size() reflects the current number of elements.operator[] retrieves the stored value.operator[].#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {10, 20, 30};
nums.pop_back();
cout << nums.size() << " " << nums[1];
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
pop_back() removes the last element and reduces size by one.pop_back() on an empty vector causes undefined behaviour.empty() to check before calling pop_back() safely.Test Incomplete