C++ 时间(mhour)操作(+/-…)

C++ 时间(mhour)操作(+/-…),c++,time,int,output,operations,C++,Time,Int,Output,Operations,谢谢阅读,这是一个简单的问题,但是。。。我很惊讶 我的代码是: /* GET THE TIME */ time_t theTime = time(0); struct tm *aTime = localtime(&theTime); int mhour = aTime->tm_hour; ostringstream oss; string myString = " "; oss << mhour; myString += oss.str(); // OKAY,

谢谢阅读,这是一个简单的问题,但是。。。我很惊讶

我的代码是:

/* GET THE TIME */ 

time_t theTime = time(0);
struct tm *aTime = localtime(&theTime);
int mhour = aTime->tm_hour;

ostringstream oss;
string myString = " ";

oss << mhour;
myString += oss.str(); // OKAY, OUTPUT Correct

std::cout << myString << endl;    

// But if i replace and execute this : 

oss << (mhour + 3);
myString += oss.str();// OUTPUT IS EMPTY ! Why ? How can i add +, -, * on "mhour" ?

为什么??我如何在mhour上添加+、-、*呢?

您希望输出到哪里?第二次时,您不会将任何内容打印到控制台

如果将这行代码放在示例的末尾:

std::cout << myString << endl;
这是有道理的,因为您在字符串中添加了两次oss,第二次它没有被清除,字符串也没有被清除


因此,您的mhour价值正如您所期望的那样增长。

您有什么建议吗?
14
141417