c++ - 在 C++ 中解析日期字符串

标签 c++ string parsing date

我儿子正在学习 C++,他的一个练习是让用户输入 DD/MM/YYY 中的日期,然后将其输出为月日年

So: 19/02/2013
Output: February 19, 2013.

我试图帮助他理解各种方式,但现在我自己也很困惑。

获取行() std::string::substr() std::string::find() std::string::find_first_of() std::string::find_last_of()

我无法完全正确地解决这些问题。

我目前尝试解析的是:

#include <iostream>
#include <string>

using namespace std;

int main (void)
{
    string date;
    string line;

    cout << "Enter a date in dd/mm/yyyy format: " << endl;
    std::getline (std::cin,date);

    while (getline(date, line))
    {
        string day, month, year;
        istringstream liness( line );
        getline( liness, day, '/' );
        getline( liness, month,  '/' );
        getline( liness, year,   '/' );

        cout << "Date pieces are: " << day << " " << month << " " << year << endl;
    }
}

但我收到如下错误:

`g++ 3_12.cpp -o 3_12`
`3_12.cpp: In function ‘int main()’:`
`3_12.cpp:16: error: cannot convert ‘std::string’ to ‘char**’ for argument ‘1’ to ‘ssize_t getline(char**, size_t*, FILE*)’`
`3_12.cpp:18: error: variable ‘std::istringstream liness’ has initializer but incomplete type`

最佳答案

int day, month, year;
char t;
std::cin >> day >> t >> month >> t >> year;

关于c++ - 在 C++ 中解析日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14966710/

相关文章:

具有不满足严格弱排序的值的 c++ 排序范围

php - PHP 中的 "Function split() is deprecated"?

parsing - 我可以从我的船长解析器收集属性吗?

Android OpenCV颜色分割显示在ImageView上

用 C/C++ 编写的 PHP 词法和语法分析器

javascript - javascript .split (' ' ) 会将逗号留在数组中吗?

python - 如何向包含特定值的行添加新列?

Java从字符串中解析长

c++ - 使用 boost::bind 和 boost::function 作为回调的类成员函数

java - 简单的 Java 正则表达式不起作用