c++ - 是否可以降低 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;
}
}
解决方案
#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));
}
}
推荐阅读
- bash - 从第一个用户输入定义的选项中接受第二个用户输入的最佳方式是什么?
- firebase - 我想在firebase中实现一个服务器端计时器
- kubernetes - 集群外的 Istio VirtualService 网络
- python - 如何解决 seaborn :RuntimeWarning: log10 中遇到的除以零?
- in-app-purchase - 每次更改代码应用内购买项目时,我是否应该将更新发送到商店?
- qpid - 如何列出 qpid 队列配置信息?
- powershell - Get-MsalToken 不包括令牌中的受众声明。使用 Get-MsalToken 时如何在令牌中包含受众声明?
- unity3d - Unity 布娃娃问题
- r - 如何将分组的数据框转换为另一个数据框?
- windows-10 - 当我启动 Windows 10 Hyper-V Linux 虚拟机时,没有外部 Internet 连接,我如何通过 SSH 和 HTTP 连接到它?