【问题标题】:Date Format Manipulation(YYYY/MM/DD-to-MM/DD/YYYY)日期格式操作(YYYY/MM/DD-to-MM/DD/YYYY)
【发布时间】:2013-10-14 01:18:08
【问题描述】:

好的,所以我使用这种字符串流技术从 .txt 文件中提取对象的类型、日期、分数等,然后稍后在我的代码中将其推回向量中。 我的问题是输入文件中的日期为 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);
}

【问题讨论】:

    标签: c++ vector formatting substring


    【解决方案1】:

    这是可以做到的方式之一:

    //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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      相关资源
      最近更新 更多