我可以格式化 Int
以显示带前导零的数字,但我不知道如何将带零的 Int
保存到 字符串
.
原因:正在加载以 "..0001""..0002"... "..0059"
等结尾的图像文件
我有这个,但它不起作用:
int a;
for(int i = 1; i < imgArraySize + 1; i++)
{
cout << setw(4) << setfill('0') << i << '\n';
cin >> a;
string aValue = to_string(a);
imageNames.push_back(string("test_images" + aValue + ".jpg"));
}
最佳答案
您可以使用 stringstream 应用相同的格式
std::ostringstream ss;
ss << std::setw(4) << std::setfill('0') << a;
std::string str = ss.str();
std::cout << str;
关于c++将带有前导零的Int保存到String,而不显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26486419/