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

实测 <utility> pair/CTAD/结构化绑定、swap/move/forward、integer_sequence 与实战案例

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-libs-utility.cpp
explore-cpp-libs-utility.cpp158 行
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <tuple>
#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 '\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

namespace {

std::string value_category(int&) { return "lvalue"; }
std::string value_category(int&&) { return "rvalue"; }

template <typename T>
std::string forward_probe(T&& arg) {
    return value_category(std::forward<T>(arg));
}

template <typename T, T... Ints>
constexpr T sum_sequence(std::integer_sequence<T, Ints...>) {
    return (Ints + ... + 0);
}

std::pair<double, bool> calculateSqrt(double number) {
    if (number < 0.0) {
        return std::make_pair(0.0, false);
    }
    return std::make_pair(std::sqrt(number), true);
}

class SimpleDictionary {
    std::vector<std::pair<std::string, std::string>> entries;
public:
    void addWord(const std::string& w, const std::string& m) {
        entries.emplace_back(w, m);
    }
    std::pair<bool, std::string> findMeaning(const std::string& word) const {
        for (const auto& [w, m] : entries) {
            if (w == word) return std::make_pair(true, m);
        }
        return std::make_pair(false, "");
    }
};

} // namespace

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

    // Case 1: std::pair, std::make_pair, C++17 CTAD, comparison, tie, structured binding
    std::pair<int, std::string> s1(101, "Alice");
    auto s2 = std::make_pair(102, std::string("Bob"));
    std::pair s3(103, std::string("Charlie")); // C++17 CTAD
    std::pair<int, double> p1(10, 3.14);
    std::pair<int, double> p2(20, 2.71);
    bool cmp_ok = (p1 < p2);
    p1 = p2;
    int a = 0;
    double b = 0.0;
    std::tie(a, b) = p1;
    auto [x, y] = p1;
    bool c1 = (s1.first == 101 && s1.second == "Alice") &&
              (s2.first == 102 && s2.second == "Bob") &&
              (s3.first == 103 && s3.second == "Charlie") &&
              cmp_ok && (a == 20 && b == 2.71) && (x == 20 && y == 2.71);
    cases.push_back({"pair_make_pair_ctad_and_binding", lab::jbool(true), lab::jbool(c1), c1});

    // Case 2: std::swap, std::move, std::forward
    int i1 = 10, i2 = 20;
    std::swap(i1, i2);
    std::string st1 = "Hello", st2 = "World";
    std::swap(st1, st2);
    std::vector<int> v1 = {1, 2, 3, 4, 5};
    const int* orig_ptr = v1.data();
    std::vector<int> v2 = std::move(v1);
    int lv = 10;
    bool c2 = (i1 == 20 && i2 == 10) &&
              (st1 == "World" && st2 == "Hello") &&
              (v2.size() == 5 && v2.data() == orig_ptr && v1.empty()) &&
              (forward_probe(lv) == "lvalue") &&
              (forward_probe(20) == "rvalue");
    cases.push_back({"swap_move_and_forward", lab::jbool(true), lab::jbool(c2), c2});

    // Case 3: std::integer_sequence & std::make_integer_sequence (C++14 + C++17 fold)
    constexpr int sum1 = sum_sequence(std::integer_sequence<int, 1, 2, 3, 4, 5>{});
    constexpr int sum2 = sum_sequence(std::make_integer_sequence<int, 5>{});
    bool c3 = (sum1 == 15) && (sum2 == 10);
    cases.push_back({"integer_sequence_compile_time", lab::jbool(true), lab::jbool(c3), c3});

    // Case 4: Practical pair return, std::map::emplace, and SimpleDictionary
    auto [r1, ok1] = calculateSqrt(16.0);
    auto [r2, ok2] = calculateSqrt(-4.0);
    std::map<int, std::string> mp;
    mp.insert(std::make_pair(101, "Alice"));
    mp.emplace(102, "Bob");
    SimpleDictionary dict;
    dict.addWord("apple", "fruit");
    dict.addWord("book", "reading");
    auto found = dict.findMeaning("apple");
    auto missing = dict.findMeaning("none");
    bool c4 = ok1 && (std::fabs(r1 - 4.0) < 1e-12) && !ok2 && (r2 == 0.0) &&
              (mp.size() == 2 && mp[101] == "Alice" && mp[102] == "Bob") &&
              (found.first && found.second == "fruit") && (!missing.first);
    cases.push_back({"practical_pair_return_map_and_dict", lab::jbool(true), lab::jbool(c4), c4});

    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-utility-lab/explore-cpp-libs-utility.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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