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

实测 <algorithm> 排序搜索、复制修改、全排列归并及有序集合与极值运算

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-libs-algorithm.cpp
explore-cpp-libs-algorithm.cpp172 行
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#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"; }

std::string join_ints(const std::vector<int>& v) {
    std::string out;
    for (std::size_t i = 0; i < v.size(); ++i) {
        if (i) out += ",";
        out += std::to_string(v[i]);
    }
    return out;
}

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: sort, partial_sort, stable_sort, find, find_if, binary_search
    std::vector<int> nums = {5, 2, 9, 1, 5, 6};
    std::sort(nums.begin(), nums.end());
    std::vector<int> pnums = {9, 4, 7, 1, 3, 2};
    std::partial_sort(pnums.begin(), pnums.begin() + 3, pnums.end());
    std::vector<std::pair<int, char>> stable_v = {{2, 'a'}, {1, 'b'}, {2, 'c'}, {1, 'd'}};
    std::stable_sort(stable_v.begin(), stable_v.end(),
                     [](const auto& x, const auto& y) { return x.first < y.first; });
    auto it_find = std::find(nums.begin(), nums.end(), 6);
    auto it_gt3 = std::find_if(nums.begin(), nums.end(), [](int x) { return x > 3; });
    bool bfound = std::binary_search(nums.begin(), nums.end(), 5);
    std::string stable_order;
    for (const auto& p : stable_v) stable_order += p.second;
    std::string act1 = "sort=" + lab::join_ints(nums)
        + ";top3=" + std::to_string(pnums[0]) + "," + std::to_string(pnums[1]) + "," + std::to_string(pnums[2])
        + ";stable=" + stable_order
        + ";find=" + std::to_string(*it_find)
        + ";find_if=" + std::to_string(*it_gt3)
        + ";bsearch=" + std::to_string(bfound ? 1 : 0);
    std::string exp1 = "sort=1,2,5,5,6,9;top3=1,2,3;stable=bdac;find=6;find_if=5;bsearch=1";
    cases.push_back({
        "algo_sort_and_search",
        lab::esc(exp1),
        lab::esc(act1),
        act1 == exp1
    });

    // Case 2: copy, equal, reverse, fill, replace
    std::vector<int> src = {1, 2, 3, 4, 5};
    std::vector<int> dst(5);
    std::copy(src.begin(), src.end(), dst.begin());
    bool eq = std::equal(src.begin(), src.end(), dst.begin());
    std::reverse(dst.begin(), dst.end());
    std::replace(dst.begin(), dst.end(), 1, 99);
    std::vector<int> filled(4);
    std::fill(filled.begin(), filled.end(), 7);
    std::string act2 = "eq=" + std::to_string(eq ? 1 : 0)
        + ";mod=" + lab::join_ints(dst)
        + ";fill=" + lab::join_ints(filled);
    std::string exp2 = "eq=1;mod=5,4,3,2,99;fill=7,7,7,7";
    cases.push_back({
        "algo_copy_equal_modify",
        lab::esc(exp2),
        lab::esc(act2),
        act2 == exp2
    });

    // Case 3: next_permutation, prev_permutation, merge, inplace_merge
    std::vector<int> perm = {1, 2, 3};
    int perm_cnt = 0;
    do { ++perm_cnt; } while (std::next_permutation(perm.begin(), perm.end()));
    // after loop perm wrapped to {1,2,3}; prev_permutation wraps to {3,2,1}
    std::prev_permutation(perm.begin(), perm.end());
    std::vector<int> v1 = {1, 3, 5};
    std::vector<int> v2 = {2, 4, 6};
    std::vector<int> merged(6);
    std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), merged.begin());
    std::vector<int> ip = {1, 4, 7, 2, 5, 8};
    std::inplace_merge(ip.begin(), ip.begin() + 3, ip.end());
    std::string act3 = "perms=" + std::to_string(perm_cnt)
        + ";prev=" + lab::join_ints(perm)
        + ";merge=" + lab::join_ints(merged)
        + ";ip=" + lab::join_ints(ip);
    std::string exp3 = "perms=6;prev=3,2,1;merge=1,2,3,4,5,6;ip=1,2,4,5,7,8";
    cases.push_back({
        "algo_permutation_and_merge",
        lab::esc(exp3),
        lab::esc(act3),
        act3 == exp3
    });

    // Case 4: set_union, set_intersection, set_difference, for_each, min/max_element, accumulate
    std::vector<int> a = {1, 2, 3, 5};
    std::vector<int> b = {2, 3, 4, 6};
    std::vector<int> u(8), inter(8), diff(8);
    u.resize(static_cast<std::size_t>(std::set_union(a.begin(), a.end(), b.begin(), b.end(), u.begin()) - u.begin()));
    inter.resize(static_cast<std::size_t>(std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), inter.begin()) - inter.begin()));
    diff.resize(static_cast<std::size_t>(std::set_difference(a.begin(), a.end(), b.begin(), b.end(), diff.begin()) - diff.begin()));
    std::vector<int> fe = {1, 2, 3};
    std::for_each(fe.begin(), fe.end(), [](int& x) { x += 1; });
    int acc = std::accumulate(fe.begin(), fe.end(), 0);
    int mn = *std::min_element(u.begin(), u.end());
    int mx = *std::max_element(u.begin(), u.end());
    std::string act4 = "u=" + lab::join_ints(u)
        + ";i=" + lab::join_ints(inter)
        + ";d=" + lab::join_ints(diff)
        + ";fe=" + lab::join_ints(fe)
        + ";sum=" + std::to_string(acc)
        + ";min=" + std::to_string(mn)
        + ";max=" + std::to_string(mx);
    std::string exp4 = "u=1,2,3,4,5,6;i=2,3;d=1,5;fe=2,3,4;sum=9;min=1;max=6";
    cases.push_back({
        "algo_set_ops_and_minmax",
        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-libs-algorithm-lab/explore-cpp-libs-algorithm.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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