这个使用sprintf的Perl代码的C++等价物是什么

What is the C++ equivalent of this Perl code that uses sprintf?

本文关键字:代码 C++ 等价物 是什么 Perl sprintf      更新时间:2023-10-16

我需要模拟以下Perl代码的功能

if($file =~ /^([^_]+)_([^_]+)_MA_(d{4})(dd)(dd)_(dd)(dd)(dd).MA$/) {
  my ($radar, $beam, $year, $month, $day, $hour, $min, $sec) =
      ($1, $2, $3, $4, $5, $6, $7, $8);
  my $file_ts = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
      $year, $month, $day, $hour, $min, $sec);
  # ...
}

在C++中。我该用哪个函数?

好吧,总有使用sprintf:的选项

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

或者您可以使用stringstream,作为更具C++风格的替代方案。

与Perl中的相同:

char file_ts[1000];
snprintf(file_ts, "%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec);