#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
struct PersonStruct {
std::string name;
int age;
};
class PersonClass {
private:
std::string name_;
int age_;
public:
PersonClass(std::string n, int a) : name_(std::move(n)), age_(a) {}
std::string format() const { return name_ + ":" + std::to_string(age_); }
};
struct ListNode {
int data;
ListNode* next;
};
int main() {
// 1. Array, struct, and class foundations
int arr[5] = {1, 2, 3, 4, 5};
PersonStruct ps{"Alice", 25};
PersonClass pc("Bob", 30);
std::string act1 = std::to_string(arr[0]) + "," + std::to_string(arr[4]) + "," +
ps.name + ":" + std::to_string(ps.age) + "," + pc.format();
const std::string exp1 = "1,5,Alice:25,Bob:30";
bool ok1 = (act1 == exp1);
// 2. Manual singly linked list insert, traverse, and complete cleanup
ListNode* head = nullptr;
for (int v : {30, 20, 10}) {
head = new ListNode{v, head};
}
std::string act2;
int freed = 0;
for (ListNode* cur = head; cur != nullptr; cur = cur->next) {
if (!act2.empty()) act2 += "->";
act2 += std::to_string(cur->data);
}
while (head != nullptr) {
ListNode* next = head->next;
delete head;
head = next;
++freed;
}
act2 += "|freed=" + std::to_string(freed);
const std::string exp2 = "10->20->30|freed=3";
bool ok2 = (act2 == exp2);
// 3. Stack (LIFO), Queue (FIFO), Deque (both ends)
std::stack<int> st;
st.push(1);
st.push(2);
int st_top = st.top();
st.pop();
std::queue<int> q;
q.push(1);
q.push(2);
int q_front = q.front();
q.pop();
std::deque<int> dq;
dq.push_back(1);
dq.push_front(2);
int dq_front = dq.front();
int dq_back = dq.back();
dq.pop_front();
std::string act3 = "st=" + std::to_string(st_top) + ",q=" + std::to_string(q_front) +
",dq=" + std::to_string(dq_front) + "/" + std::to_string(dq_back) +
",dq_after=" + std::to_string(dq.front());
const std::string exp3 = "st=2,q=1,dq=2/1,dq_after=1";
bool ok3 = (act3 == exp3);
// 4. unordered_map, map (ordered), set (unique ordered), vector
std::unordered_map<std::string, int> umap;
umap["apple"] = 10;
umap["pear"] = 20;
std::map<std::string, int> omap;
omap["zebra"] = 3;
omap["apple"] = 1;
omap["mango"] = 2;
std::string map_keys;
for (const auto& kv : omap) {
if (!map_keys.empty()) map_keys += ",";
map_keys += kv.first;
}
std::set<int> oset = {3, 1, 2, 1, 3};
std::vector<int> vec;
vec.push_back(10);
vec.push_back(20);
std::string act4 = "umap=" + std::to_string(umap["apple"]) + ",omap=" + map_keys +
",set_sz=" + std::to_string(oset.size()) + ",set_min=" + std::to_string(*oset.begin()) +
",vec0=" + std::to_string(vec[0]);
const std::string exp4 = "umap=10,omap=apple,mango,zebra,set_sz=3,set_min=1,vec0=10";
bool ok4 = (act4 == exp4);
std::cout << "{\n"
<< " \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
<< " \"cases\": [\n"
<< " {\"name\": \"array_struct_class_basics\", \"expected\": \"" << exp1
<< "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
<< " {\"name\": \"singly_linked_list_ops\", \"expected\": \"" << exp2
<< "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
<< " {\"name\": \"stack_queue_deque_adaptors\", \"expected\": \"" << exp3
<< "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
<< " {\"name\": \"hash_map_ordered_map_set_vector\", \"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-data-structures-lab/explore-cpp-data-structures.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-data-structures.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2316.zip
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip