C++ gives you two very different ways to store a list of items: the plain, old-fashioned array inherited from C, and std::vector, a much friendlier tool from the Standard Template Library. Almost all real, modern C++ code reaches for vectors, and once you see why, you'll understand.

The plain array: fixed, and a little unforgiving

int scores[5] = {90, 85, 70, 60, 100};
std::cout << scores[0];  // 90

A plain array has a size fixed at the moment it's created, and it won't stop you from reading or writing past its end — scores[10] on a five-item array won't throw a clean error, it will just quietly read whatever happens to be sitting in memory at that spot, which is exactly the kind of bug that has caused real, serious security problems in C++ software over the decades.

std::vector: the safer, more flexible option

#include <vector>

std::vector<int> scores = {90, 85, 70, 60};
scores.push_back(100);          // grow it
std::cout << scores[0];         // 90
std::cout << scores.size();     // 5

A vector can grow and shrink, just like a Python list or a Java ArrayList, and it keeps track of its own size for you, so you never have to pass a separate length variable alongside it the way you sometimes do with plain arrays.

Looping through a vector

for (int score : scores) {
    std::cout << score << " ";
}

This range-based for loop, added to C++ relatively recently in the language's long history, reads almost identically to a for-each loop in Java or Python, and it's genuinely the nicest way to loop over a vector when you don't need the index itself.

What the STL actually is

vector is just one small piece of the Standard Template Library, a huge collection of ready-made data structures and algorithms that ship with C++. Rather than writing your own sorting function, for example, the STL hands you one already:

#include <algorithm>

std::sort(scores.begin(), scores.end());

Why vectors won over plain arrays

  • They resize automatically as items are added or removed.
  • They know their own size, so you aren't tracking a separate length variable by hand.
  • They work seamlessly with the rest of the STL's algorithms, like sort, find and count.

Plain arrays haven't disappeared entirely — they still show up, especially in older code or in situations where performance needs to be squeezed as tightly as possible — but for everyday C++ written today, reaching for a vector first is the sensible default, and plain arrays become the exception rather than the rule.