好的,所以我使用这种字符串流技术从 .txt 文件中提取对象的类型、日期、分数等,并稍后在我的代码中将其推回到 vector 中。 我的问题是输入文件中的日期格式为 YYYY/MM/DD,我需要将其切换为 MM/DD/YYYY,但我不熟悉如何操作。有人告诉我使用子字符串方法可行,但我是 C++ 的新手,所以有人知道如何解决我的这个问题吗?
void LineData :: Set_Line (const string s)
{
stringstream temp (s);
temp >> type;
temp >> date;
string max;
temp >> max;
max_score = atoi (max.c_str());
string actual;
temp >> actual;
actual_score = atof (actual.c_str());
ws(temp);
getline(temp, name);
}
最佳答案
这是可以完成的方法之一:
//this will split-up the string
int i = 0;
stringstream ssdate(date); //date needs to be stringstream
while (getline(ssdate, part, '/'))
{
if(i == 0) year = part;
if(i == 1) month = part;
if(i == 2) day = part;
i++; //counter
}
//now all you have to do is arrage them how you need them
//If you want me to show you that I can
关于c++ - 日期格式操作(YYYY/MM/DD-to-MM/DD/YYYY),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19352197/