蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 STL 五大组件:vector 动态扩容与迭代器、三类容器、排序变换仿函数及容器适配器

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-stl-tutorial.cpp
explore-cpp-stl-tutorial.cpp150 行
#include <algorithm>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace lab {

struct Case {
    std::string name;
    std::string expected_json;
    std::string actual_json;
    bool passed;
};

std::string esc(const std::string& s) {
    std::string out = "\"";
    for (unsigned char c : s) {
        switch (c) {
            case '\"': out += "\\\""; break;
            case '\\': out += "\\\\"; break;
            case '\b': out += "\\b"; break;
            case '\f': out += "\\f"; break;
            case '\n': out += "\\n"; break;
            case '\r': out += "\\r"; break;
            case '\t': out += "\\t"; break;
            default:
                if (c < 0x20) {
                    char buf[8];
                    std::snprintf(buf, sizeof(buf), "\\u%04x", c);
                    out += buf;
                } else {
                    out += static_cast<char>(c);
                }
        }
    }
    out += "\"";
    return out;
}

std::string jbool(bool v) { return v ? "true" : "false"; }

void emit(const std::vector<Case>& cases) {
    std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
    for (std::size_t i = 0; i < cases.size(); ++i) {
        if (i) std::cout << ", ";
        std::cout << "{\"name\": " << esc(cases[i].name)
                  << ", \"expected\": " << cases[i].expected_json
                  << ", \"actual\": " << cases[i].actual_json
                  << ", \"passed\": " << jbool(cases[i].passed) << "}";
    }
    std::cout << "]}\n";
}

} // namespace lab

int main() {
    std::vector<lab::Case> cases;

    // Case 1: std::vector dynamic growth & iterator traversal
    std::vector<int> vec;
    std::size_t init_sz = vec.size();
    for (int i = 0; i < 5; ++i) vec.push_back(i);
    std::ostringstream oss;
    oss << init_sz << "->" << vec.size() << ":";
    for (auto it = vec.begin(); it != vec.end(); ++it) {
        if (it != vec.begin()) oss << ",";
        oss << *it;
    }
    std::string act1 = oss.str();
    std::string exp1 = "0->5:0,1,2,3,4";
    cases.push_back({
        "stl_vector_growth_and_iterators",
        lab::esc(exp1),
        lab::esc(act1),
        act1 == exp1
    });

    // Case 2: Sequence, associative, and unordered containers
    std::deque<int> dq = {2, 3};
    dq.push_front(1);
    dq.push_back(4);
    std::list<int> lst = {10, 20, 30};
    std::set<int> st = {3, 1, 2, 2};
    std::multiset<int> mst = {3, 1, 2, 2};
    std::map<std::string, int> mp = {{"a", 1}, {"b", 2}};
    std::multimap<std::string, int> mmp = {{"a", 1}, {"a", 2}};
    std::unordered_set<int> ust = {10, 20, 10};
    std::unordered_map<std::string, int> ump = {{"k", 9}};
    bool cont_ok = (dq.front() == 1 && dq.back() == 4) &&
                   (lst.size() == 3) &&
                   (st.size() == 3 && *st.begin() == 1) &&
                   (mst.size() == 4 && mst.count(2) == 2) &&
                   (mp["b"] == 2 && mmp.count("a") == 2) &&
                   (ust.size() == 2 && ump["k"] == 9);
    cases.push_back({
        "stl_sequence_associative_unordered",
        lab::jbool(true),
        lab::jbool(cont_ok),
        cont_ok
    });

    // Case 3: Algorithms + function objects (std::greater & lambda predicate)
    std::vector<int> nums = {4, 1, 5, 2, 3};
    std::sort(nums.begin(), nums.end(), std::greater<int>());
    auto gt2 = std::count_if(nums.begin(), nums.end(), [](int x) { return x > 2; });
    std::vector<int> doubled(nums.size());
    std::transform(nums.begin(), nums.end(), doubled.begin(), [](int x) { return x * 2; });
    bool alg_ok = (nums == std::vector<int>{5, 4, 3, 2, 1}) &&
                  (gt2 == 3) &&
                  (doubled == std::vector<int>{10, 8, 6, 4, 2});
    cases.push_back({
        "stl_algorithms_and_function_objects",
        lab::jbool(true),
        lab::jbool(alg_ok),
        alg_ok
    });

    // Case 4: Container adapters (stack, queue, priority_queue)
    std::stack<int> stk;
    stk.push(1); stk.push(2); stk.push(3);
    std::queue<int> q;
    q.push(1); q.push(2); q.push(3);
    std::priority_queue<int> pq;
    pq.push(20); pq.push(50); pq.push(10);
    std::ostringstream ad;
    ad << stk.top() << "," << q.front() << "," << pq.top();
    std::string act4 = ad.str();
    std::string exp4 = "3,1,50";
    cases.push_back({
        "stl_container_adapters",
        lab::esc(exp4),
        lab::esc(act4),
        act4 == exp4
    });

    lab::emit(cases);
    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-stl-tutorial-lab/explore-cpp-stl-tutorial.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-stl-tutorial.cpp,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C++

课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip

下载课程关联工程包:C++

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2395.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习