Definition
📌 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)..
Syntax
syntax
type arrayName[size] = {values};
#include <vector>
vector<type> name = {values};Example
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;
}Output
output90 85 70 100
Important Points
- C-style arrays have a fixed size decided at compile time.
vectorresizes automatically as you add or remove elements.vector.size()gives the current number of elements — no manual bookkeeping needed.- Both support indexing with
[], starting at 0.
Common Mistakes
- Using a C-style array when the number of elements isn't known ahead of time — use
vectorinstead. - Forgetting
#include <vector>. - Accessing an index out of range —
vectorwon't stop you with[](use.at()for bounds-checked access).
Interview Tip
🎯 Before your interview
Interviewers may ask "why prefer vector over a raw array?" — dynamic sizing, automatic memory management, and built-in helper methods.
