用c plus plus 写一个函数,输入是一个字符串“2023-4-23”,将他解析成2023年4月23号,并与当前年月日作比较,判断他是否在当前年月日之后
时间: 2024-05-09 08:21:50 浏览: 7
好的,我明白了。根据您的要求,以下是一个用c plus plus写的函数:
```c++
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
bool isAfter(string inputDate) {
//获取当前时间
time_t now = time(nullptr);
tm *localTime = localtime(&now);
// 解析字符串为年、月、日
int year = stoi(inputDate.substr(0, 4));
int month = stoi(inputDate.substr(5, 2));
int day = stoi(inputDate.substr(8, 2));
// 判断是否在当前年月日之后
if (year > localTime->tm_year + 1900) {
return true;
}
else if (year == localTime->tm_year + 1900) {
if (month > localTime->tm_mon + 1) {
return true;
}
else if (month == localTime->tm_mon + 1) {
if (day >= localTime->tm_mday) {
return true;
}
}
}
return false;
}
int main() {
string inputDate = "2023-4-23";
bool result = isAfter(inputDate);
cout << result << endl;
return 0;
}
```
该函数接受一个形如“2023-4-23”的字符串作为输入,解析并与当前年月日作比较,返回一个bool型结果,表示该日期是否在当前日期之后。如果返回true,则日期在当前日期之后,否则返回false。