Post

Modern-CPP - Chapter08 - String & Stream

Modern-CPP - Chapter08 - String & Stream

课程来源:b站我是龙套小果丁

Tips

  • 第七题如果使用std::range_formatter编译失败,可以使用统一的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <typename T> struct std::formatter<List<T>> {
  // We need to parse format specifiers
  constexpr auto parse(std::format_parse_context &ctx) { return ctx.begin(); }

  // Format the list
  auto format(const List<T> &list, std::format_context &ctx) const {
    auto out = ctx.out();
    out = std::format_to(out, "[");

    bool first = true;
    for (const auto &item : list) {
      if (!first) {
        out = std::format_to(out, "=>");
      }
      out = std::format_to(out, "{}", item);
      first = false;
    }

    return std::format_to(out, "]");
  }
};
  • 在MacOS上zh-CN可能需要被替换为zh-CN.UTF-8
This post is licensed under CC BY 4.0 by the author.