解析C+中的日期字符串+; 我儿子正在学习C++,他的一个练习是让用户输入DD/MM/YYY中的日期,然后输出到月日、年< /P> So: 19/02/2013 Output: February 19, 2013.

解析C+中的日期字符串+; 我儿子正在学习C++,他的一个练习是让用户输入DD/MM/YYY中的日期,然后输出到月日、年< /P> So: 19/02/2013 Output: February 19, 2013.,c++,string,parsing,date,C++,String,Parsing,Date,我试图帮助他理解各种方法,但现在我把自己弄糊涂了 getline() 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) {

我试图帮助他理解各种方法,但现在我把自己弄糊涂了

getline()
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;
    }
}
你错过了比赛!我认为这是最安全、最有效的方法


回到主题上来,我认为既然
getline
是一个
extern“C”
函数,就不能使用
使用命名空间std来重载它(顺便说一句,这应该被禁止)。您应该尝试为所有
getline
调用预先设置
std

对于
std::istringstream
,您需要:

#include <sstream>
#包括
注意:不要使用
名称空间标准。这是一个坏习惯,最终会给你带来麻烦。

错误

int day, month, year;
char t;
std::cin >> day >> t >> month >> t >> year;
3\u 12.cpp:16:错误:无法将参数“1”的“std::string”转换为“char**”转换为“ssize\t getline(char**,size\t*,FILE*)”

表示编译器无法理解这一行:

while (getline(date, line))
date
line
都声明为
std::string
,并且
getline
没有重载 需要两个字符串。编译器猜测你试图调用它不是C++库的一部分(很显然,标准库头之一包括<代码> STDIO。H/COD>这是函数来自哪里)。 错误

3\u 12.cpp:18:错误:变量'std::istringstream'具有初始值设定项,但类型不完整

意味着编译器不知道什么是
std::istringstream
。您忘记包含

如果希望通过
std::getline
从字符串中提取行,则需要首先将其放入stringstream中,就像在循环中一样

解析日期并不是那么容易。您不希望用户能够输入
44/33/-200
,然后逍遥法外。我将如何处理这个问题:

std::istringstream iss(date); // date is the line you got from the user

unsigned int day, month, year;
char c1, c2;

if (!(iss >> day >> c1 >> month >> c2 >> year)) {
    // error
}
if (c1 != '/' || c2 != '/') { /* error */ }
if (month > 12) { /* error  / }

// ... more of that, you get the idea

你没有帮你的孩子做他的工作。谢谢Austin,但他坐在我旁边看着医生也在想办法。所以我称之为团队工作,我想补充一点,这是他放学的一周,也是课外活动。
size\t getline(char**,size\t*,FILE*)
是一个很好的例子。我不认为试图称之为你的意图,或者说是吗?@AustinMullins和你的孩子们坐在一起工作是一种极大的乐趣这是我的快速尝试。Hw确实是用更好的方法来做这件事的。
std::
这不是一种有趣的方法@杰森,我会认为它是唯一的(从一种呈现的方式):/ReGEX是过多的,正如代码< STD::String < /Calp> S和<代码> STD::GETLION>代码> ReGEX,您可以免费获得验证。代码>标准::字符串
由于短字符串优化,在大多数实现中是免费的。@AlexChamberlain在什么上下文中免费提供,计算成本肯定不低。一个好的正则表达式的复杂性可以很容易地用代码来模拟。坦白说,我们这里所说的不是低延迟的玩弄技巧,而是针对年轻程序员的一般编程建议。字符串的C++习惯用法是:代码> STD::String ,在大多数情况下,它几乎是免费的。Regex-s在解决许多问题方面都非常强大,这是一个非常好的应用程序。
std::istringstream iss(date); // date is the line you got from the user

unsigned int day, month, year;
char c1, c2;

if (!(iss >> day >> c1 >> month >> c2 >> year)) {
    // error
}
if (c1 != '/' || c2 != '/') { /* error */ }
if (month > 12) { /* error  / }

// ... more of that, you get the idea