我正在尝试创建一个String包含数字的数组.这些数字是我需要访问的文件夹的名称.目前我宣布它如下所示:
String str1[] = { "001", "002", "003", "004", "005", "006", "007", "008", "009", "010", "011", "012", "013", "014", "015", "016", "017", "018", "019", "020", };
Run Code Online (Sandbox Code Playgroud)
我有124个文件夹,并以这种方式命名它们是乏味的.有一个更好的方法吗?我正在使用C++.
您可以使用字符串流并设置格式选项以将整数填充到特定数量的字符并设置填充字符.
编辑:好吧我的代码不是从1开始但是0,但我相信你可以想出来:)
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
std::vector<std::string> strs;
for (int i = 0; i < 124; i++)
{
std::ostringstream os;
os << std::setfill('0') << std::setw(3) << i;
strs.push_back(os.str());
}
for (const auto& s : strs)
{
std::cout << s << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)