3

我可以格式化 anInt以显示带有前导零的数字,但我不知道如何将Int带有零的 an 保存到 a string

"..0001" "..0002" ... "..0059"原因:加载以etc结尾的图像文件。

我有这个,但它不起作用:

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"));
}
4

1 回答 1

4

您可以使用字符串流应用相同的格式

std::ostringstream ss;
ss << std::setw(4) << std::setfill('0') << a;
std::string str = ss.str();
std::cout << str;

Live example

于 2014-10-21T12:15:07.037 回答