Skip to content

C++ 首批课程:值语义、RAII与库存汇总项目 ​

读取库存记录时可以借用当前输入行来解析名称,最终的汇总表却要跨行保存名称。先回顾值与资源所有权和容器与迭代器,再对照项目里 string_view 和 string 的使用位置,判断哪一步需要建立新的拥有者。

环境与完整起步 ​

项目采用 C++20 和标准库。示例命令使用 Zig 的 C++ 驱动,也可用支持 C++20 的 G++ 或 Clang++。Linux x86_64 上用 G++ 15.2 编译了本页三个独立程序和库存项目测试,测试输出 inventory tests passed。

保存hello.cpp:

cpp
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
int main()
{
    std::vector<std::string> names{"C++", "RAII"};
    auto copy = names;
    copy.push_back("test");
    const auto& borrowed = names;
    assert(names.size() == 2 && copy.size() == 3 && borrowed.size() == 2);
    for (const auto& name : names) std::cout << name << '\n';
}

Windows执行zig c++ -std=c++20 -Wall -Wextra -Werror hello.cpp -o hello.exe再运行.\hello.exe;Linux输出hello并运行./hello。预期两行C++、RAII。不要使用zig cc把C++源码当C链接;语言前端、标准和标准库是一组工具链约定。

变量先初始化,auto推断静态类型,不是动态类型。函数按值接收时会创建参数值,按引用接收时使用已有对象;只读的较大对象常用const T&。有符号整数溢出不是可依赖的环绕;类型范围与容器长度用对应标准接口检查。

值、引用与容器生存期 ​

hello里的copy拥有自己的vector/string元素;borrowed是原vector的别名,不复制它。引用必须绑定有效对象,不能返回局部对象的引用。vector会按需要扩容,导致原指针/引用/迭代器失效;不要取得元素引用后再push_back并继续使用旧引用。at可检查越界并抛异常,operator[]仍要求索引有效。

string拥有字符存储;string_view只是视图,不延长拥有者生存期。下面view.cpp可独立运行,owner在使用view期间一直存活且没有使它失效的修改:

cpp
#include <cassert>
#include <string>
#include <string_view>
int main()
{
    std::string owner = "book";
    std::string_view view = owner;
    assert(view.substr(0, 2) == "bo");
    std::string independent(view);
    owner = "pen";
    assert(independent == "book");
}

owner赋新内容后不再读取view。标准字符串按字符单元存储字节序列,.size()不等于所有Unicode文字的视觉字符数。接口返回新结果时,通常返回拥有数据的string/vector;不为避免一次复制而返回悬空view。

RAII、移动与类的职责 ​

RAII把资源释放与对象析构联系起来;ifstream、vector、unique_ptr都可管理各自资源。离开作用域或异常展开时析构负责清理,不用在每个return前复制一遍delete或close。GC不是C++的默认资源管理模型;对象可以在不同存储区域,其析构时机由生存期规则决定。

保存ownership.cpp:

cpp
#include <cassert>
#include <memory>
#include <utility>
int main()
{
    auto owned = std::make_unique<int>(42);
    auto moved = std::move(owned);
    assert(!owned && *moved == 42);
}

预期无输出、退出0。unique_ptr不能复制;移动转移其所有权,不能再解引用空的owned。std::move本身是转换表达式,实际是否移动由目标操作决定;一般移动后的对象处于有效但未指定状态,不能把unique_ptr保证为空的特性推广到所有类型。普通业务类优先组合已有资源对象,遵循Rule of Zero,避免无需求地同时编写五个特殊成员函数。

容器、算法与异常接口 ​

vector用于顺序元素,map用于按键有序映射;unordered_map不承诺相同迭代顺序。算法配合迭代器与谓词工作;本项目用all_of验证名称,用from_chars严格解析整数。from_chars不隐式吞掉所有尾随字符,必须同时看错误码与结束指针。不要把stoi("12x")的前缀解析当成完整输入校验。

头文件声明接口,.cpp实现它,调用者在另一源文件中使用。模板定义通常需要在实例化处可见;本批不引入模块或模板库工程。函数的失败约定必须一致:库抛有意义异常,CLI入口统一记录并转换退出码。异常不是必然崩溃,捕获后也不能返回空结果伪装成功。

完整库存记录汇总CLI ​

路径examples/cpp/inventory-summary。教学格式每行name,quantity,没有引号/转义,不是通用CSV。name为1–32个ASCII字母、数字、下划线或短横线且首字符为字母;quantity为0–100000的无符号十进制,不接受空白、正负号或尾随字符。单行最多256字节、10000条、1000个名称,每个名称累计最多1000000。接受LF/CRLF,空文件输出为空且成功;空行失败。

inventory.hpp
cpp
#ifndef INVENTORY_HPP
#define INVENTORY_HPP
#include <istream>
#include <map>
#include <string>
#include <stdexcept>
using Inventory = std::map<std::string, unsigned long>;
Inventory read_inventory(std::istream& input);
#endif
inventory.cpp
cpp
#include "inventory.hpp"
#include <algorithm>
#include <charconv>
#include <string_view>

static bool valid_name(std::string_view name)
{
    auto letter = [](char c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); };
    return !name.empty() && name.size() <= 32 && letter(name.front()) &&
        std::all_of(name.begin(), name.end(), [&](char c) {
            return letter(c) || (c >= '0' && c <= '9') || c == '_' || c == '-';
        });
}

Inventory read_inventory(std::istream& input)
{
    Inventory result;
    std::string line;
    std::size_t row = 0;
    while (std::getline(input, line)) {
        ++row;
        if (!line.empty() && line.back() == '\r') line.pop_back();
        auto fail = [&] { throw std::invalid_argument("row " + std::to_string(row) + ": invalid inventory record"); };
        if (line.size() > 256 || row > 10000) fail();
        const auto comma = line.find(',');
        if (comma == std::string::npos || line.find(',', comma + 1) != std::string::npos) fail();
        std::string_view name(line.data(), comma);
        std::string_view digits(line.data() + comma + 1, line.size() - comma - 1);
        unsigned long quantity = 0;
        auto [end, error] = std::from_chars(digits.data(), digits.data() + digits.size(), quantity);
        if (!valid_name(name) || digits.empty() || error != std::errc{} || end != digits.data() + digits.size() || quantity > 100000) fail();
        std::string key(name);
        if (!result.contains(key) && result.size() >= 1000) fail();
        auto& total = result[key];
        if (quantity > 1000000 - total) fail();
        total += quantity;
    }
    if (input.bad() || (input.fail() && !input.eof())) throw std::runtime_error("input read failure");
    return result;
}
main.cpp
cpp
#include "inventory.hpp"
#include <fstream>
#include <iostream>

int main(int argc, char** argv)
{
    if (argc != 2) { std::cerr << "usage: inventory-summary FILE\n"; return 2; }
    try {
        std::ifstream input(argv[1], std::ios::binary);
        if (!input) throw std::runtime_error("cannot open file");
        const auto inventory = read_inventory(input);
        for (const auto& [name, quantity] : inventory) std::cout << name << '=' << quantity << '\n';
        std::cout.flush();
        if (!std::cout) throw std::runtime_error("output failure");
        return 0;
    } catch (const std::exception& error) {
        std::cerr << "error: " << error.what() << '\n';
        return 1;
    }
}
test_inventory.cpp
cpp
#include "inventory.hpp"
#include <cassert>
#include <sstream>
#include <iostream>

int main()
{
    std::istringstream input("book,2\r\npen,1\nbook,3\n");
    auto result = read_inventory(input);
    assert(result.at("book") == 5 && result.at("pen") == 1);
    std::istringstream empty("");
    assert(read_inventory(empty).empty());
    std::istringstream unreadable("book,1\n");
    unreadable.setstate(std::ios::failbit);
    bool read_failed = false;
    try { (void)read_inventory(unreadable); }
    catch (const std::runtime_error&) { read_failed = true; }
    assert(read_failed);
    for (const auto* text : {"book,-1\n", "book,1x\n", "book,100001\n", "bad name,1\n", "book,1,2\n", "\n"}) {
        std::istringstream bad(text);
        bool caught = false;
        try { (void)read_inventory(bad); }
        catch (const std::invalid_argument&) { caught = true; }
        assert(caught);
    }
    std::ostringstream too_many;
    for (int i = 0; i < 11; ++i) too_many << "book,100000\n";
    std::istringstream overflow(too_many.str());
    bool caught = false;
    try { (void)read_inventory(overflow); }
    catch (const std::invalid_argument&) { caught = true; }
    assert(caught);
    std::cout << "inventory tests passed\n";
}
sample.txt
text
book,2
pen,1
book,3

目录内编译zig c++ -std=c++20 -Wall -Wextra -Werror inventory.cpp main.cpp -o inventory-summary.exe,Windows执行.\inventory-summary.exe sample.txt;Linux去.exe并执行./inventory-summary sample.txt。预期按名称排序输出:

text
book=5
pen=1

测试编译inventory.cpp与test_inventory.cpp,运行test-inventory,预期inventory tests passed。不加会关闭assert的-DNDEBUG。覆盖重复聚合、空文件、非法字段和累计超限;实际CLI另验证参数、缺文件、坏行、CRLF和失败时stdout为空。先完成解析再输出,避免部分输出被下游误当成完整结果。

ifstream持有文件资源,read_inventory只借用istream,调用者拥有并负责其生存期;库函数返回map值。name的string_view仅在当前line不变时使用,并在下一次getline之前构造拥有键的string。输入逐行读取后检查长度,因此不是对任意超大单行都有固定内存上限的流式协议解析器。

诊断、退出码与边界 ​

成功0写stdout;无参数或多参数2向stderr写usage;缺文件、格式/累计超限或读失败1向stderr写error。相对输入路径基于启动目录。Windows窄字符路径转换受环境影响,本批使用普通文件名,不宣称任意路径编码均验证。

库在循环后拒绝 badbit,以及没有正常EOF的 failbit;预先失败的输入流不能当空文件成功。CLI输出后刷新并检查cout,输出失败转为退出1;已输出的前缀不能回滚。POSIX默认SIGPIPE可能直接终止进程,这里没有修改信号策略,不能把所有管道错误都承诺成退出1。

undefined reference先检查所有.cpp是否参与链接;no matching function先核对参数类型和const;unique_ptr复制报错应检查所有权是否本来需要转移,不用shared_ptr消灭所有诊断。异常处理不会修复悬空引用等未定义行为,必须先遵守生存期规则。高级内存检查、并发、模板元编程另批验证。

练习与自检 ​

  1. 用pen,0和重复book输入预测结果。2. 增加按数量降序显示,数量相同时按名称,解释为何不能只换unordered_map。3. 指出项目里拥有资源和借用资源的变量。4. 比较按值返回string与返回局部string_view的安全性。
提示与参考解法

0是合法数量,新名称仍会得到0条目;book重复累加。排序显示可把map条目复制到vector<pair<string,unsigned long>>,用sort比较数量再比较名称,不改变解析约定。ifstream拥有文件,istream&借用它;line/string/map拥有字符和节点,string_view借用当前line。按值返回string拥有结果,可使用返回值优化/移动;指向局部string的view离开函数后悬空,不能靠const修复。

依据:C++ Core Guidelines、标准C++库与资源管理FAQ、中文参考。核对2026-10-17;中文资料用于对照,关键规则以标准与实现文档核对。

继续阅读