gpt4 book ai didi

c++ - 如何在 C++ 中使用 stringstream 从字符串中提取时间

转载 作者:行者123 更新时间:2023-11-30 02:37:51 30 4
gpt4 key购买 nike

如何使用 stringstream 从类似于此的输入中提取小时、分钟、秒和 AM/PM 字符串?

输入:

07:05:45PM

最佳答案

大致是这样的:

unsigned int hour;
unsigned int minute;
unsigned int second;
char colon1;
char colon2;
string AMPM;

if (stream >> hour >> colon1 >>minute >> colon2 >> second >> AMPM)
{
if (colon1 == ':' && colon2 == ':')
{
if ((hour < 12) && (minute < 60) && (second < 60))
{
if (AMPM == "AM")
{
// do nothing
}
else if (AMPM == "PM")
{
hour += 12;

}
else
{
//freak out
}
}
else
{
//freak out
}
}
else
{
//freak out
}
}
else
{
//freak out
}

关于c++ - 如何在 C++ 中使用 stringstream 从字符串中提取时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31422823/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com