【发布时间】:2021-12-16 23:15:55
【问题描述】:
当我试图通过 to_string 函数将给定的整数转换为字符串时,它只是省略了整数的前导零。
为什么?以及如何克服这个问题?
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
string s = to_string(n);
cout << s;
}
【问题讨论】:
-
将其保存为字符串。整数没有“前导零”(或者可能有无限多个)。
-
但我必须在我的字符串中取零。那我该怎么办?
-
将其读入
std::string变量而不是int。
标签: c++ type-conversion