Standard Library
Vector Basics
std::vector stores a growable sequence of values.
vector
A vector keeps values in order and can grow when you add another element.
Vector Basics
vector_basics.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <vector>
int main() {
int extraScore = 15;
std::vector<int> scores{10, 20};
scores.push_back(extraScore);
int total = 0;
for (int score : scores) {
total += score;
}
std::cout << "count=" << scores.size() << std::endl;
std::cout << "last=" << scores.back() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#include <vector>
int main() {
int extraScore = 5;
std::vector<int> scores{10, 20};
scores.push_back(extraScore);
int total = 0;
for (int score : scores) {
total += score;
}
std::cout << "count=" << scores.size() << std::endl;
std::cout << "last=" << scores.back() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#include <vector>
int main() {
int extraScore = 25;
std::vector<int> scores{10, 20};
scores.push_back(extraScore);
int total = 0;
for (int score : scores) {
total += score;
}
std::cout << "count=" << scores.size() << std::endl;
std::cout << "last=" << scores.back() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
extraScore ← 15, scores ← [10, 20], total ← 0
4int main() {5 int extraScore→ 15 = 15; //@extraScore=5, 2567 std::vector<int> scores→ [10, 20]{10, 20};8 scores→ [10, 20, 15].push_back(extraScore15);910 int total→ 0 = 0;11 for (int score : scores) {total ← 10
pass 1 of 310int total = 0;11for (int score10 : scores[10, 20, 15]) {12 total→ 10 += score10;13}All 3 passes — pass 1 is the card above pass scoretotal1 10 0 → 10 2 20 10 → 30 3 15 30 → 45 std::cout << "count=" << scores.size() << std::endl;
15 std::cout << "count=" << scores[10, 20, 15].size() << std::endl;16 std::cout << "last=" << scores[10, 20, 15].back() << std::endl;17 std::cout << "total=" << total45 << std::endl;18 return 0;19}outputcount=3 last=15 total=45
extraScore ← 5, scores ← [10, 20], total ← 0
4int main() {5 int extraScore→ 5 = 5;67 std::vector<int> scores→ [10, 20]{10, 20};8 scores→ [10, 20, 5].push_back(extraScore5);910 int total→ 0 = 0;11 for (int score : scores) {total ← 10
pass 1 of 310int total = 0;11for (int score10 : scores[10, 20, 5]) {12 total→ 10 += score10;13}All 3 passes — pass 1 is the card above pass scoretotal1 10 0 → 10 2 20 10 → 30 3 5 30 → 35 std::cout << "count=" << scores.size() << std::endl;
15 std::cout << "count=" << scores[10, 20, 5].size() << std::endl;16 std::cout << "last=" << scores[10, 20, 5].back() << std::endl;17 std::cout << "total=" << total35 << std::endl;18 return 0;19}outputcount=3 last=5 total=35
extraScore ← 25, scores ← [10, 20], total ← 0
4int main() {5 int extraScore→ 25 = 25;67 std::vector<int> scores→ [10, 20]{10, 20};8 scores→ [10, 20, 25].push_back(extraScore25);910 int total→ 0 = 0;11 for (int score : scores) {total ← 10
pass 1 of 310int total = 0;11for (int score10 : scores[10, 20, 25]) {12 total→ 10 += score10;13}All 3 passes — pass 1 is the card above pass scoretotal1 10 0 → 10 2 20 10 → 30 3 25 30 → 55 std::cout << "count=" << scores.size() << std::endl;
15 std::cout << "count=" << scores[10, 20, 25].size() << std::endl;16 std::cout << "last=" << scores[10, 20, 25].back() << std::endl;17 std::cout << "total=" << total55 << std::endl;18 return 0;19}outputcount=3 last=25 total=55