首页 > 解决方案 > 是否可以降低 cout 打印的速度?

问题描述

我有兴趣在cout以下程序中查看计算机处理命令输出的方式(即它如何通过每一行的各种迭代)。是否可以降低该过程的速度?

#include <iomanip>
#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{
    for (int x=1; x <= 12; x++)
    {
        for (int y=1; y <= 12; y++)
            cout << setw(4) << x*y; cout << endl;
    }
}

标签: c++cout

解决方案


#include <iomanip>
#include <iostream>
#include <thread>       // std::this_thread::sleep_for

// using namespace std; // dont get used to using this

int main() {            // if you are not using the arguments, leave it like this
    for(int x = 1; x <= 12; x++) {
        for(int y = 1; y <= 12; y++) std::cout << std::setw(4) << x * y;
        std::cout << '\n'; // just some advice: replace all your "endl"s with '\n'
                           // until flushing is needed.

        // here's one way to make it slower that WhozCraig mentioned in a comment:
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

推荐阅读