int转换成string的几种方法

来源:互联网 发布:中联软件靠谱吗 编辑:程序博客网 时间:2024/05/21 07:53

第一种:

#include <sstream>std::string list::toString(void) const {    node* positioner = this->head;    std::string result;    std::stringstream ss;    while (positioner != NULL) {            ss << positioner->data;            std::string temp;            ss >> temp;        result += temp + "->";        ss.clear();        positioner = positioner->next;    }    result += "NULL";    return result;}

第二种:

std::string Date::toStr() const {    long long y = year;    long long m = month;    long long d = day;    std::string r =  std::to_string(y) + "-";    if (month < 10)        r += "0";    r += std::to_string(m) + "-";    if (day < 10)    r += "0";    r += std::to_string(d);    return r;}

第三种:

std::string Date::toStr() const {    char ch[10];    std::string str;    snprintf(ch, sizeof(ch), "%d", year);    str += ch;    str += "-";    if (month < 10)        str += '0';    snprintf(ch, sizeof(ch), "%d", month);    str += ch;    str += "-";    if (day < 10)        str += '0';    snprintf(ch, sizeof(ch), "%d", day);    str += ch;    return str;}
0 0
原创粉丝点击