Daq*_*aqs 2 c++ string int datetime itoa
以下代码以时间格式输出值,即如果是 1:50pm 和 8 秒,则将其输出为 01:50:08
cout << "time remaining: %02d::%02d::%02" << hr << mins << secs;
Run Code Online (Sandbox Code Playgroud)
但我想要做的是(a)将这些整数转换为字符/字符串(b),然后将相同的时间格式添加到其相应的字符/字符串值中。
我已经实现了(a),我只想实现(b)。
例如
char currenthour[10] = { 0 }, currentmins[10] = { 0 }, currentsecs[10] = { 0 };
itoa(hr, currenthour, 10);
itoa(mins, currentmins, 10);
itoa(secs, currentsecs, 10);
Run Code Online (Sandbox Code Playgroud)
现在,如果我输出 'currenthour'、'currentmins' 和 'currentsecs',它将输出与 1:50:8 相同的示例时间,而不是 01:50:08。
想法?
如果您不介意开销,您可以使用 std::stringstream
#include <sstream>
#include <iomanip>
std::string to_format(const int number) {
std::stringstream ss;
ss << std::setw(2) << std::setfill('0') << number;
return ss.str();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14839 次 |
| 最近记录: |