简体   繁体   English

c ++使用前导零保存Int到String,不显示它们

[英]c++ Save Int with leading zeros to String, NOT displaying them

I can format an Int to display a number with leading zeros, but I can't figure out how to save an Int with the zeros to a string . 我可以格式化一个Int来显示一个带前导零的数字,但是我无法弄清楚如何用一个零来保存一个Int到一个string

Reason: Loading image files that end in "..0001" "..0002" ... "..0059" etc. 原因:加载以"..0001" "..0002" ... "..0059"等结尾的图像文件

I have this, but it doesn't work: 我有这个,但它不起作用:

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

You can apply the same formatting with a stringstream 您可以使用stringstream应用相同的格式

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

Live example 实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM