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

实测 std::string 声明拼接、越界保护 at()、子串查找替换、修改与 C 互操作及字符集搜索

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-libs-string.cpp
explore-cpp-libs-string.cpp149 行
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <string>
#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: declaration, init, +, size/length/empty, operator[], at()
    std::string empty_s;
    std::string s1 = "Hello, ";
    std::string s2 = "World!";
    std::string greeting = s1 + s2;
    bool threw_at = false;
    try {
        (void)greeting.at(99);
    } catch (const std::out_of_range&) {
        threw_at = true;
    }
    std::string act1 = "empty=" + std::to_string(empty_s.empty() ? 1 : 0)
        + ";sz=" + std::to_string(greeting.size())
        + ";len=" + std::to_string(greeting.length())
        + ";c0=" + std::string(1, greeting[0])
        + ";c7=" + std::string(1, greeting.at(7))
        + ";oob=" + std::to_string(threw_at ? 1 : 0);
    std::string exp1 = "empty=1;sz=13;len=13;c0=H;c7=W;oob=1";
    cases.push_back({
        "string_init_concat_and_access",
        lab::esc(exp1),
        lab::esc(act1),
        act1 == exp1
    });

    // Case 2: substr, find, rfind, replace
    std::string sub = greeting.substr(7, 5);
    std::size_t pos_w = greeting.find("World");
    std::size_t rpos_l = greeting.rfind("l");
    std::string modified = greeting;
    if (pos_w != std::string::npos) {
        modified.replace(pos_w, 5, "C++");
    }
    std::string act2 = "sub=" + sub
        + ";find=" + std::to_string(pos_w)
        + ";rfind=" + std::to_string(rpos_l)
        + ";mod=" + modified;
    std::string exp2 = "sub=World;find=7;rfind=10;mod=Hello, C++!";
    cases.push_back({
        "string_substr_find_replace",
        lab::esc(exp2),
        lab::esc(act2),
        act2 == exp2
    });

    // Case 3: append, insert, erase, clear, c_str, data, compare
    std::string work = modified;
    work.append(" How are you?");
    work.insert(7, " Beautiful");
    std::string after_ins = work;
    work.erase(7, 10);
    std::string after_era = work;
    work.clear();
    bool cleared = work.empty();
    work = "Hello, C++!";
    bool cstr_ok = (std::strcmp(work.c_str(), "Hello, C++!") == 0)
        && (std::memcmp(work.data(), "Hello, C++!", work.size()) == 0)
        && (work.compare("Hello, C++!") == 0)
        && (work.compare("Z") < 0);
    std::string act3 = "ins=" + after_ins + "|era=" + after_era
        + "|clr=" + std::to_string(cleared ? 1 : 0)
        + "|cstr=" + std::to_string(cstr_ok ? 1 : 0);
    std::string exp3 = "ins=Hello,  BeautifulC++! How are you?|era=Hello, C++! How are you?|clr=1|cstr=1";
    cases.push_back({
        "string_modify_and_c_interop",
        lab::esc(exp3),
        lab::esc(act3),
        act3 == exp3
    });

    // Case 4: find_first_of, find_last_of, find_first_not_of, find_last_not_of
    std::string target = "Hello, C++!";
    std::size_t p1 = target.find_first_of("aeiou");
    std::size_t p2 = target.find_last_of("aeiou");
    std::size_t p3 = target.find_first_not_of("aeiou");
    std::size_t p4 = target.find_last_not_of("aeiou");
    std::string act4 = std::to_string(p1) + "," + std::to_string(p2) + "," + std::to_string(p3) + "," + std::to_string(p4);
    std::string exp4 = "1,4,0,10";
    cases.push_back({
        "string_char_set_search",
        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-string-lab/explore-cpp-libs-string.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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