Chapter 5: Arrays, Vectors, and Your Final Project
You've reached the final chapter of this C++ beginner course. You now understand syntax, object-oriented basics, control flow, and functions with references and pointers. This chapter ties it all together by introducing collections of data — arrays and the more flexible std::vector — and then challenges you to build a small project using everything you've learned.
1. C-Style Arrays
An array stores a fixed number of elements of the same type in contiguous memory. Once declared, its size cannot change, which is its biggest limitation.
int scores[5] = {90, 85, 78, 92, 88};
std::cout << scores[0] << std::endl; // 90
std::cout << scores[4] << std::endl; // 88
2. The Power of std::vector
In modern C++, std::vector from the Standard Template Library is almost always preferred over a raw array. A vector can grow or shrink at runtime, and it manages its own memory automatically, which removes a whole category of bugs that plague C-style arrays.
#include <vector>
std::vector<int> grades = {90, 85, 78};
grades.push_back(95); // add an element
std::cout << grades.size() << std::endl; // 4
for (int g : grades) {
std::cout << g << std::endl;
}
3. Combining Vectors with Classes
Vectors become especially powerful when storing objects rather than primitives. This lets you model real collections of things — a list of students, products, or game characters — using the class concepts from Chapter 2.
class Student {
public:
std::string name;
int score;
};
std::vector<Student> classroom;
classroom.push_back({"Aman", 91});
classroom.push_back({"Riya", 88});
for (const Student& s : classroom) {
std::cout << s.name << ": " << s.score << std::endl;
}
4. Final Project: Student Grade Tracker
Put everything together by building a simple Student Grade Tracker. Create a Student class with a name and a vector of scores. Write a method that calculates and returns the average score using a range-based for loop. Then create a std::vector<Student> to hold several students, add a few using pointers or references to avoid unnecessary copying, and print each student's name alongside their average grade.
Common Beginner Mistakes
- Out-of-bounds access: reading or writing past the end of an array or vector causes undefined behavior instead of a clean error.
- Copying large vectors unnecessarily: pass vectors by reference (
const std::vector<T>&) into functions instead of by value to avoid expensive copies. - Forgetting
#include <vector>before trying to usestd::vector.
Where to Go Next
You now have a solid foundation in C++: syntax, OOP, control flow, functions, references, and collections. From here, explore pointers and dynamic memory in more depth, look into the rest of the STL (maps, sets, and algorithms), and try building a larger project such as a simple inventory or library management system.
Why Prefer Vectors Over Arrays
Unlike plain C-style arrays, std::vector can grow and shrink dynamically, tracks its own size, and comes with useful built-in methods — making it the go-to choice in modern C++.
#include <vector>
vector<int> scores = {90, 85, 78};
scores.push_back(95); // add an item
cout << scores.size(); // 4
cout << scores.at(0); // 90
Iterating with Range-Based For Loops
Modern C++ lets you loop through a vector without manually managing an index variable:
for (int score : scores) {
cout << score << " ";
}
CodeMaster Academy