有什么方法可以使它运行得更快并且仍然做同样的事情吗?
#include <iostream>
int box[80][20];
void drawbox()
{
for(int y = 0; y < 20; y++)
{
for(int x = 0; x < 80; x++)
{
std::cout << char(box[x][y]);
}
}
}
int main(int argc, char* argv[])
{
drawbox();
return(0);
}
IDE:开发 C++ ||操作系统:Windows
最佳答案
正如 Marc B 在评论中所说,先将输出放入字符串应该更快:
int box[80][20];
void drawbox()
{
std::string str = "";
str.reserve(80 * 20);
for(int y = 0; y < 20; y++)
{
for(int x = 0; x < 80; x++)
{
str += char(box[x][y]);
}
}
std::cout << str << std::flush;
}
关于c++ - 我怎样才能使 cout 更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4789346/