#include <iostream>
#include <numeric>
#include <stdexcept>
#include <string>
#include <vector>
struct TrackedPerson {
std::string name;
int age;
static int copy_or_move_count;
TrackedPerson(std::string n, int a) : name(std::move(n)), age(a) {}
TrackedPerson(const TrackedPerson& o) : name(o.name), age(o.age) { ++copy_or_move_count; }
TrackedPerson(TrackedPerson&& o) noexcept : name(std::move(o.name)), age(o.age) { ++copy_or_move_count; }
};
int TrackedPerson::copy_or_move_count = 0;
int main() {
// 1. Initialization forms, push_back, operator[] vs at() bounds check
std::vector<int> v_zero(5);
std::vector<int> v_fill(5, 10);
std::vector<int> v_list = {1, 2, 3, 4};
v_list.push_back(100);
bool caught_oor = false;
try {
(void)v_list.at(99);
} catch (const std::out_of_range&) {
caught_oor = true;
}
std::string act1 = std::to_string(v_zero[4]) + "," + std::to_string(v_fill[2]) + "," +
std::to_string(v_list[0]) + "," + std::to_string(v_list.at(4)) + "," +
std::to_string(v_list.size()) + "," + (caught_oor ? "oor" : "none");
const std::string exp1 = "0,10,1,100,5,oor";
bool ok1 = (act1 == exp1);
// 2. size vs capacity, reserve() avoiding reallocation, clear() vs swap()
std::vector<int> unreserved;
int reallocs_unreserved = 0;
const int* prev_ptr = nullptr;
for (int i = 0; i < 64; ++i) {
unreserved.push_back(i);
if (unreserved.data() != prev_ptr) {
++reallocs_unreserved;
prev_ptr = unreserved.data();
}
}
std::vector<int> reserved;
reserved.reserve(64);
const int* base_ptr = reserved.data();
int reallocs_reserved = 0;
for (int i = 0; i < 64; ++i) {
reserved.push_back(i);
if (reserved.data() != base_ptr) {
++reallocs_reserved;
}
}
std::size_t cap_before_clear = reserved.capacity();
reserved.clear();
std::size_t size_after_clear = reserved.size();
std::size_t cap_after_clear = reserved.capacity();
std::vector<int>().swap(reserved);
std::size_t cap_after_swap = reserved.capacity();
bool ok2 = (reallocs_unreserved > 1) && (reallocs_reserved == 0) &&
(size_after_clear == 0) && (cap_after_clear == cap_before_clear) &&
(cap_after_swap == 0);
// 3. Iterators, erase shifting, front insert, and safe re-indexing after growth
std::vector<int> seq = {1, 2, 3, 4, 5};
seq.erase(seq.begin() + 1); // remove 2 -> {1, 3, 4, 5}
seq.insert(seq.begin(), 100); // -> {100, 1, 3, 4, 5}
int sum_idx = 0, sum_it = 0, sum_rng = 0;
for (std::size_t i = 0; i < seq.size(); ++i) sum_idx += seq[i];
for (auto it = seq.begin(); it != seq.end(); ++it) sum_it += *it;
for (int x : seq) sum_rng += x;
std::string act3 = std::to_string(sum_idx) + "=" + std::to_string(sum_it) + "=" +
std::to_string(sum_rng) + ":" + std::to_string(seq[2]);
const std::string exp3 = "113=113=113:3";
bool ok3 = (act3 == exp3);
// 4. emplace_back vs push_back copy/move count & student scores example
std::vector<TrackedPerson> people;
people.reserve(4);
TrackedPerson::copy_or_move_count = 0;
people.push_back(TrackedPerson("Tom", 20));
int moves_push = TrackedPerson::copy_or_move_count;
TrackedPerson::copy_or_move_count = 0;
people.emplace_back("Alice", 22);
int moves_emplace = TrackedPerson::copy_or_move_count;
std::vector<int> scores = {85, 90, 78, 92};
scores.push_back(88);
int total = std::accumulate(scores.begin(), scores.end(), 0);
int avg_x10 = (total * 10) / static_cast<int>(scores.size()); // 866 -> 86.6
scores.erase(scores.begin() + 2); // {85, 90, 92, 88}
std::string act4 = "push=" + std::to_string(moves_push) + ",emplace=" + std::to_string(moves_emplace) +
",avg10=" + std::to_string(avg_x10) + ",rem=" + std::to_string(scores.size()) +
",third=" + std::to_string(scores[2]);
const std::string exp4 = "push=1,emplace=0,avg10=866,rem=4,third=92";
bool ok4 = (act4 == exp4);
std::cout << "{\n"
<< " \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
<< " \"cases\": [\n"
<< " {\"name\": \"vector_init_push_and_at_bounds\", \"expected\": \"" << exp1
<< "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
<< " {\"name\": \"vector_capacity_reserve_and_clear\", \"expected\": true, \"actual\": "
<< (ok2 ? "true" : "false") << ", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
<< " {\"name\": \"vector_iterators_insert_and_erase\", \"expected\": \"" << exp3
<< "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
<< " {\"name\": \"vector_emplace_and_scores_workflow\", \"expected\": \"" << exp4
<< "\", \"actual\": \"" << act4 << "\", \"passed\": " << (ok4 ? "true" : "false") << "}\n"
<< " ]\n"
<< "}\n";
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cpp-vector-lab/explore-cpp-vector.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-vector.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2404.zip