第一种:
#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;
}