简体   繁体   English

time_t(mhour)操作(+ /-…)

[英]time_t (mhour) operations (+ / - …)

thank's for read, this is easy question but ... i'am surprised. 感谢您的阅读,这是一个简单的问题,但是...我很惊讶。

My code is : 我的代码是:

/* 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" ?

Why ? 为什么呢 How can i add +, -, * on "mhour" ? 如何在“ mhour”上添加+,-,*?

Where do you expect your output? 您期望在哪里输出? You don't print anything to the console the second time around. 您第二次无需在控制台上打印任何内容。

If you put this line of code at the end of your sample: 如果将此代码行放在示例的末尾:

std::cout << myString << endl;

Then the total output of your program will be (for example): 然后,您的程序的总输出为(例如):

14
141417

This makes sense, as you add oss twice to your string, and the second time it hasn't been cleared, nor has your string. 这是有道理的,因为您将oss两次添加到字符串中,并且第二次未清除它,您的字符串也未清除。

So your value of mhour is increasing as you expect. 因此,您的mhour价值正在按您的期望增加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM