Lesson 7 • Intermediate
Arrays & Vectors
Store and manipulate collections of data — from fixed-size arrays to dynamic, resizable vectors.
What You'll Learn
- ✓ Declaring and using C-style arrays
- ✓ Vectors: dynamic arrays with built-in methods
- ✓ Sorting, searching, and iterating collections
- ✓ 2D arrays and multi-dimensional data
Arrays vs Vectors
Both arrays and vectors store multiple values of the same type. Think of an array like a fixed row of lockers — you decide the number at build time and can't add more. A vector is like a magic expanding shelf — it grows as needed.
| Feature | Array | Vector |
|---|---|---|
| Size | Fixed at compile time | Dynamic (grows/shrinks) |
| Include | Built-in | #include <vector> |
| Bounds checking | ❌ No | ✅ .at() |
| Add elements | ❌ Cannot | ✅ .push_back() |
| Know size | Manual tracking | .size() |
| Best for | Fixed, small data | Most use cases |
C-Style Arrays
Create arrays, iterate, and calculate averages and extremes
#include <iostream>
using namespace std;
int main() {
// Declaring and initializing arrays
int scores[5] = {95, 87, 73, 100, 68};
string days[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};
// Accessing elements (0-indexed)
cout << "First score: " << scores[0] << endl; // 95
cout << "Last score: " << scores[4] << endl; // 68
// Modifying elements
scores[2] = 85; // Change 73 to 85
// Looping through an array
cout << "\n=== All Scores ===" <
...Vectors
Use push_back, sort, reverse, and find with dynamic vectors
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
// Creating vectors
vector<int> numbers = {10, 20, 30, 40, 50};
vector<string> fruits; // Empty vector
// Adding elements
fruits.push_back("Apple");
fruits.push_back("Banana");
fruits.push_back("Cherry");
fruits.push_back("Date");
// Size and capacity
cout << "Fruits count: " << fruits.size() << endl;
cout << "Is empty: " << fruits.empty() << endl;
...2D Arrays & Matrices
Work with grids, matrices, and 2D vectors for student grades
#include <iostream>
#include <vector>
using namespace std;
int main() {
// 2D array — grid / matrix
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Print the matrix
cout << "=== 3x3 Matrix ===" << endl;
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << matrix[row][col] << " ";
}
cout << endl;
}
// Sum of all elements
int total = 0;
for (int r = 0
...Common Mistakes
⚠️ Out-of-bounds access: Accessing arr[5] on a size-5 array is undefined behavior. Arrays are 0-indexed (0 to size-1).
⚠️ Array size must be constant: int arr[n] with a variable n is not standard C++. Use vectors instead.
⚠️ Forgetting #include <vector>: Vectors aren't built-in — you need the header.
⚠️ Passing arrays to functions: Arrays decay to pointers — always pass the size separately or use vectors.
Pro Tips
💡 Prefer vectors: In modern C++, vectors are almost always better than raw arrays. Use std::array for fixed sizes.
💡 Reserve capacity: If you know the size upfront, call vec.reserve(n) to avoid repeated reallocation.
💡 Use range-based for: for (int x : vec) is cleaner and less error-prone than index-based loops.
📋 Quick Reference
| Operation | Syntax |
|---|---|
| Declare array | int arr[5] = {1,2,3,4,5}; |
| Declare vector | vector<int> v = {1,2,3}; |
| Add element | v.push_back(4); |
| Remove last | v.pop_back(); |
| Size | v.size() |
| Sort | sort(v.begin(), v.end()); |
Lesson Complete!
You can now store and manipulate collections of data with arrays and vectors. Next up: Pointers & References — unlock the power of memory management.
Sign up for free to track which lessons you've completed and get learning reminders.