You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否存在std::format的逆向函数?寻求高效C++字符串解析方案

高性能反向格式化与时间字符串解析需求

正向格式化示例(参考)

已知可通过标准库实现时间到字符串的格式化(其中tm为std::tm类型):

const auto time = std::chrono::system_clock::from_time_t(std::mktime(&tm));
const std::string s = std::format("{:%d %b %Y}", time);
// 示例输出s: 20 March 1990

核心需求

我需要以下两种方案之一:

  1. 存在类似std::make_time_point的函数,传入时间字符串和格式化规则直接得到time_point,示例如下:
const std::string data = "20 March 1990";
const auto time = std::make_time_point("{:%d %b %Y}", data);
  1. 支持通用反向格式化的函数,能将字符串按指定格式拆分到对应变量中,示例如下:
int a = 0;
std::string b;
int c = 0;
const std::string data = "20 March 1990";
std::reverse_format("{} {} {}", data, &a, &b, &c);
// 执行后结果: a = 20, b = "March", c = 1990

关键限制:必须避免使用stringstream,因为要处理海量数据,stringstream会导致性能大幅下降。目前我用stringview配合大量substr实现了一个不够优雅的方案,希望替换为更简洁高效的实现。

补充说明

此前该问题被标记为重复,但已有的采纳方案要么使用了stringstream(不符合性能要求),要么是非标准实现且仅适用于UNIX系统,都无法满足需求。

内容的提问来源于stack exchange,提问作者Eshy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 10:46:04