【发布时间】:2014-05-30 05:42:08
【问题描述】:
我的函数具有三个整数值,分别代表年、月和日。我想返回这些值并在它们之间加上破折号“-”,就像常见的日期格式一样。
例如:
int main() {
cout << myfunction() << endl; // this should display like this 2014-04-15
}
string myfunction() {
int year = 2014;
int month = 04;
int day = 15;
// I want to return this value like this
return year-month-day;//2014-04-15
}
有人可以帮我吗?
【问题讨论】:
-
目前给出的所有回复都忽略了 0-padding 的问题(例如 April -> 4 -> output
04)。 It's very easy and clear to do that withsprintf();使用<sstream>字符串流需要更多的工作。