【问题标题】:Return integer value as string for date formatting [duplicate]返回整数值作为日期格式的字符串[重复]
【发布时间】: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
}

有人可以帮我吗?

【问题讨论】:

标签: c++ string int


【解决方案1】:

在 C++03 中,您可以使用 std::ostringstream 类使用 operator&lt;&lt; 格式化字符串:

#include <string>
#include <sstream>
#include <iomanip>

std::string myfunction() {
    int year=2014;
    int month=04;
    int day=15;

    std::ostringstream oss;
    oss << year << "-" << std::setw(2) << std::setfill('0') 
                                                     << month << "-" << day;
    return oss.str();
           ^^^^^^^^^ // this will yield a formatted string that oss contains
}

在 C++11 中,您可以使用 std::to_stringoperator+ 连接字符串:

#include <string>

std::string myfunction() {
    int year=2014;
    int month=04;
    int day=15;

    std::string s = to_string( year) 
                    + "-" + to_string(month)
                    + "-" + to_string(day);
    return s;
}

但是to_string 不提供格式化选项。如果你想再次指定格式,你应该查看字符串流。

【讨论】:

  • 您确定 OP 要将 April 打印为 4,还是 04 会更好?
  • 改为打印零,谢谢
  • 不客气。也许to_string() 的第二个代码也需要修复?
  • 添加了关于格式化to_string的说明,谢谢
【解决方案2】:
#include <sstream>
#include <iomanip>    

...

std::stringstream ss;
ss << std::setfill('0') << std::setw(4) << year << "-"
   << std::setw(2) << month << "-" << std::setw(2) << day;
return ss.str();

【讨论】:

  • 您确定 OP 要将 April 打印为 4,还是 04 会更好?
  • @Mr.C64 谢谢。我根据您的建议编辑了答案。
【解决方案3】:

另外,如果您没有 C++11,请查看 boost::lexical_cast(...),或者如果您不想要 boost,请查看 stringstream。

stringstream ss;
ss << first << "-" << second << "-" << third << endl;
return ss.str();

必须工作。

【讨论】:

    【解决方案4】:
    ostringstream oss;
    oss << year << '-' << month << '-' << day;
    
    return oss.str();
    

    【讨论】:

      【解决方案5】:

      您可以使用字符串流。

      string myfunction(){
          int year=2014;
          int month=04;
          int day=15;
          stringstream ss;
          ss << year << "-" << month << "-" << day;
          return ss.str();
      }
      

      不要忘记包含sstream 标头。

      【讨论】:

        【解决方案6】:

        如果你不想使用 STL,你也可以求助于 C 风格的函数 _itoa。这是文档的链接:

        http://msdn.microsoft.com/en-us/library/yakksftt.aspx

        但我确实建议您像其他人建议的那样使用 stringstream。

        【讨论】:

          【解决方案7】:

          即使在 C++ 代码中,我也喜欢 printf 类格式说明符(在这种情况下,与 sprintf() 一起使用)。

          您可能需要考虑这个可编译的代码:

          #include <stdio.h>
          #include <iostream>
          #include <string>
          
          std::string ToString(int year, int month, int day) {
              char buffer[11];
              // YYYY-MM-DD
              // 1234567890
              //          10 + NUL --> 11 characters in buffer
          
              sprintf(buffer, "%04d-%02d-%02d", year, month, day);
              // sprintf_s on MSVC would be better (and it implicitly gets the dest buffer size).
          
              return buffer;
          }
          
          int main() {
              std::cout << ToString(2014, 4, 15) << std::endl;
          }
          

          根据要求,输出为2014-04-15


          PS
          如果你想使用 C++ 字符串流(来自 &lt;sstream&gt;),那么你应该注意 padding0 开头的月份和日期有一个数字。为此,请考虑this StackOverflow question 和相关答案。关键是使用setfill('0')setw(2)进行填充。

          【讨论】:

            猜你喜欢
            • 2011-09-21
            • 1970-01-01
            • 2019-02-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-05
            相关资源
            最近更新 更多