Arrays
C++ supports the same fixed-size arrays as C, but also offers safer alternatives from the standard library: std::array (fixed size, bounds-aware) and std::vector (dynamic size, can grow).
💡 Key insight
In real C++ projects, std::vector is used far more often than raw C-style arrays because it can grow and manages its own memory.
Syntax
syntax
type arrayName[size] = {values};
#include <vector>
vector<type> name = {values};Example
push_back() adds a new element to the end of a vector, growing it dynamically — something a plain array can't do.
cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> scores = {90, 85, 70};
scores.push_back(100);
for (int s : scores) {
cout << s << " ";
}
cout << endl;
return 0;
}output90 85 70 100
Try it yourself
Copy this example into the C++ compiler and run it — try changing a value and see what happens.
