c++ - 日期格式操作(YYYY/MM/DD-to-MM/DD/YYYY)

标签 c++ vector formatting substring

好的,所以我使用这种字符串流技术从 .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/

相关文章:

c++ - 不能使用我自己的使用 SFML 的 DLL

c++ - vector (容器)是否需要使用 "allocator"?

c - Eclipse C99结构初始化格式化

Java-获取n位小数

c++ - 如何在顶点着色器中使用 LookAt 矩阵

c++ - Win32 不一致的 CreateWindowEx 调用

c++ - 字符串与数字的比较,它是如何工作的

c++ - 将 vector <char> 复制到二维 vector <string> c++

C++ 类 vector 问题

python - 使用 Python 将字符串转换为 Csv 文件