A growable array: push_back, index, pop_back and loop with a range-based for.
C++ · beginner · 7 min · 14 steps · free
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> scores = {70, 85};
scores.push_back(90); // grows as needed
scores[0] = 75;
scores.pop_back();
size_t count = scores.size();
int total = 0;
for (int s : scores) { // range-based for loop
total += s;
}
cout << count << " " << total << endl;
return 0;
}scores changes from [70, 85, 90] to [75, 85, 90].count is created on the stack with the value 2.total is created on the stack with the value 0.s is created on the stack with the value 75.s changes from 75 to 85.total changes from 75 to 160.s goes out of scope and disappears.2 160.0.Answer: 3. Added at the end.
Answer: The vector itself. RAII again.