3

をフォーマットしIntて先頭にゼロを付けて数値を表示することはできますIntが、ゼロを付けてを に保存する方法がわかりません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

stringstreamで同じフォーマットを適用できます

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 に答える