C++ Starter · free course

Free course · Lesson 3 of 5

Arrays & Strings

In this lesson you will

  • Store values in std::vector and loop over them
  • Read and manipulate text with std::string

A C-style array is a fixed-size, contiguous block of memory holding values of one type. C++'s std::string and the Standard Library's std::vector build on top of that idea to give you safer, resizable alternatives for everyday use.

  • int scores[3] = {78, 91, 64}; — fixed size, decided at compile time.
  • std::vector<int> scores = {78, 91, 64}; — resizable, tracks its own length.
  • std::string supports concatenation with + and comparison with ==.
  • Range-based for (for (int s : scores)) avoids manual index bugs.

💡 Practical Skill: Preferring std::vector and std::string over raw C-style arrays and character pointers avoids the majority of beginner memory bugs.

Worked exampleFiltering Passing Scores
C++
#include <iostream>#include <vector> int main() {    std::vector<int> scores = {78, 91, 64, 55, 88};    std::vector<int> passed;     for (int score : scores) {        if (score >= 70) {            passed.push_back(score);        }    }     std::cout << "Passed (" << passed.size() << "): ";    for (int score : passed) std::cout << score << " ";    std::cout << std::endl;    return 0;}

Practice exercise

Store five market prices in a std::vector, then print the cheapest, the dearest and the total.

Key takeaways

  • Prefer std::vector and std::string to raw arrays and char*
  • Indexes run from 0 to size() - 1; at() checks the range
  • Standard algorithms such as std::sort save writing loops by hand
In the full program Module 3: Vectors, Arrays and Strings See how this topic is taught in depth, with practical activities and an assessment.

Free self-study lesson — no certificate. For the full instructor-led program, see registration options.