百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

scnlib

> 编程语言
开源

使用 scanf 来实现现代 C++

1.3K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

使用 scanf 来实现现代 C++

scnlib cpp #include #include // for std::println (C++23) int main() { // 从标准输入读取两个整数 // 附带一条消息 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 是一个现代的 C++ 库,用于替换 scanf 和 std::istream。此库试图让我们更接近于完全替换 iostreams 和 C stdio。它比 iostream 快(参见 Benchmarks),而且类型安全,不像 scanf。想想 {fmt} 或 C++20 的 std::format,但朝着相反的方向。此库是 ISO C++ 标准提案 P1729 "文本解析"的参考实现。文档 文档可以在线上找到,地址为 https://scnlib.dev。要自己构建文档,请构建由 CMake 生成的 scndocs 目标。这些目标仅在 CMake 中设置变量 SCNDOCS 时生成(如果 scnlib 是根项目,则会自动生成)。scndocs 目标需要 Doxygen、Python 3.8 或更高版本,以及 pip3 包 poxy。示例 请参阅 examples/ 文件夹中的更多示例。读取一个 std::string … 读取多个值 cpp #include int main() { auto input = std::string{"123 456 foo"}; auto result = scn::scan(input, "{} {}"); // result == true // result-range(): " foo" // 所有读取的值都可以通过使用 result-values() 来访问 auto [a, b] = result-values(); // 从剩余输入中读取 // 也可以使用 scn::ranges::subrange{result-begin(), result-end()} 作为输入 auto result2 = scn::scan(result-range(), "{}"); // result2 == true // result2-range().empty() == true // result2-value() == "foo" } 从一个更复杂的范围中读取 cpp #include #include int main() { auto result = scn::scan("123" | std::views::reverse, "{}"); // result == true // result-begin() 是一个迭代器,指向一个 reverseview // result-range() 为空 // result-value() == 321 重复读取 cpp #include #include int main() { std::vector vec{}; auto input = scn::ranges::subrange{"123 456 789"sv}; while (auto result = scn::scan(input), "{}")) { vec.pushba…

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •Blazing fast parsing of values (see Benchmarks)
  • •Modern C++ interface, featuring
  • •type safety (variadic templates, types not determined by the format
  • •convenience (ranges)
  • •ergonomics (values returned from scn::scan, no output parameters)
  • •support for modules (enable target scn::module with SCN_MODULES in CMake)
  • •"{python}"-like format string syntax
  • •Including compile-time format string checking
  • •Minimal code size increase (in user code, see Benchmarks)
  • •Usable without exceptions, RTTI, or ``s

> 标签

C++c-plus-pluscppinputio

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言