只有在程序获取所有输入后,我才可以打印输出吗?换句话说,在Java中是否有与cin.tie(0)
等效的东西?
在C++中,当我们包含cin.tie(0)
时,我们解开std::cout
和std::cin
。通过这样做,我们确保在用户提供输入后不会立即将输出刷新到控制台,而是在用户完成所有操作后刷新输出他们的投入。
考虑一个程序有3个测试用例作为输入的情况,我们的程序将输出Hello World!
。
输入值
Test Case 1
Test Case 2
Test Case 3
不使用cin.tie(0)
:程序(C++)
int main(){
int t;//holds the info of number of test cases
cin>>t;
for(int i=0;i<t;i++){
string s;
cin >>s;
cout<<"Hello World";
}
return 0;
}
安慰:Test Case 1
Hello World!
Test Case 2
Hello World!
Test Case 3
Hello World!
通过使用cin.tie(0):int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;//holds the info of number of test cases
cin>>t;
for(int i=0;i<t;i++){
string s;
cin >>s;
cout<<"Hello World";
}
return 0;
}
Test Case 1
Test Case 2
Test Case 3
Hello World!
Hello World!
Hello World!
有什么方法可以在Java中复制cin.tie(0)
?
最佳答案
从某些输入流读取某些C库(尤其是including glibc)时,它会刷新标准输出。您必须使用特定于平台的的 API调用来更改该行为(可能是通过完全缓冲输入流);我不知道此功能有任何标准/预制包装。
关于java - 等价于cin.tie(0)在Java中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63950516/