c++ - 一秒计时器

标签 c++ macos timer counter

<分区>

我需要一个每秒执行 X 次的计时器。 我做了这个,但是在程序终止之前它不会打印任何东西,我觉得这很奇怪。 如果您将 3 作为计数器,它会在 3 秒后打印所有内容,如果您选择 100,它会在 3 秒后打印所有内容。

如何让它每秒打印一次而不是在终止时立即打印?

int main()
{
    using namespace std;
    //Number to count down from
    int counter = 10;
    //When changed, a second has passed
    int second = (unsigned)time(NULL);
    //If not equal to each other, counter is printed
    int second_timer = second;
    while (counter > 0) {
        second = (unsigned)time(NULL);
        while (second != second_timer) {
            //Do something
            cout << counter-- << ", ";
            //New value is assigned to match the current second
            second_timer = second;
        }
    }
    cout << "0" << endl;
    return 0;
}

最佳答案

添加<< flush你想冲洗的地方。 IE。将您的打印输出更改为:

cout << counter-- << ", " << flush;

关于c++ - 一秒计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16844793/

相关文章:

c++ - C++ 中大于 2^32 的整数使用什么类型?

c - 创建共享库时,如何告诉链接器可执行文件中存在符号?

c++ - 依赖于 mac 的 Qt 应用程序 - 依赖 sdk 在 otool 列表中有@executable_path ...我该如何更改它?

Flash AS3定时器问题

c - 一段时间后计时器卡住

C++ : getline() function does not include last character/string when reading from a file

c++ - 使用基类指针查找派生类对象的大小

java - 如何防止mac中显示java进程图标

timer - 如何从 DS3231 RTC 获得毫秒分辨率

c++ - 相当于 DirectX 渲染状态的 OpenGL