gpt4 book ai didi

c++将带有前导零的Int保存到String,而不显示它们

转载 作者:太空狗 更新时间:2023-10-29 21:19:42 24 4
gpt4 key购买 nike

我可以格式化 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;

Live example

关于c++将带有前导零的Int保存到String,而不显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26486419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com