使用 scanf 来实现现代 C++
#include
#include // for std::println (C++23)
int main() {
// Read two integers from stdin
// with an accompanying message
if (auto result =
scn::prompt("What are your two favorite numbers? ", "{} {}")) {
auto [a, b] = result->values();
std::println("Oh, cool, {} and {}!", a, b);
} else {
std::println(stderr, "Error: {}", result.error().msg());
}
}请在 编译器浏览器 中尝试。
scnlib 是一个用于替换 scanf 和 std::istream 的现代 C++ 库。
这本书库试图让我们更接近于取代 iostream。
以及 C stdio。
它比 iostream 更快(参见 Benchmarks),而且类型安全,与 scanf 不同。
想想 {fmt} 或 C++20 的 std::format,但它在
另一个方向。
此库是 ISO C++ 标准提案的参考实现
P1729 "文本解析"。
文档可以在 https://scnlib.dev. 上找到。
要自己构建文档,请构建 CMake 生成的 scn_docs 目标。这些目标仅在 CMake 中设置了变量 SCN_DOCS 时才会生成(如果 scnlib 是根项目,则会自动生成)。scn_docs 目标需要 Doxygen、Python 3.8 或更高版本,以及 pip3 包 poxy。
暂无开放 Issues,或尚未同步最近议题。