Lesson 5 / 10

Arrays, Vectors and Strings

Fixed-size arrays

int scores[3] = {90, 85, 78};nstd::cout << scores[0];   // 90nstd::cout << sizeof(scores) / sizeof(scores[0]);   // 3 -- there's no built-in .length

A raw C++ array has no built-in way to ask its own length — you either track the size separately or use the somewhat awkward sizeof trick above. This is exactly the kind of rough edge std::vector was introduced to smooth over.

std::vector — resizable

#include <vector>nnstd::vector<std::string> courses;ncourses.push_back("C++");ncourses.push_back("PHP");nstd::cout << courses[0];        // C++nstd::cout << courses.size();    // 2

Prefer std::vector over a raw array in almost all real code — it resizes itself automatically as you add items, it knows its own length through .size(), and it manages its own memory safely, freeing it automatically when the vector goes out of scope.

Strings

std::string name = "Tutoline";nstd::string greeting = "Hello, " + name + "!";nstd::cout << name.length();   // 8nstd::cout << name.substr(0, 4);   // "Tuto"

std::string supports the + operator for concatenation directly, unlike raw C-style character arrays, and comes with a large set of built-in methods (.length(), .substr(), .find(), and more) that make everyday text handling far more pleasant than it was in older C++ code.

Iterating with a range-based for loop

for (const std::string& course : courses) {n    std::cout << course << std::endl;n}

This visits every element of courses without manual indexing. const std::string& avoids copying each string just to read it — a small performance habit worth building early, and one you’ll see used throughout professional C++ code.