我读到可以通过更改缓冲区通过 ifstream 读取 cin,但它安全吗?这样做的缺点是什么:
file.ios::rdbuf(cin.rdbuf());
谢谢
C++: UNIX - defaulting ifstream variable to standard input
iostream - C++: assign cin to an ifstream variable? - Stack Overflow
最佳答案
是的,它是安全的,这就是 rdbuf
函数存在的全部原因。
这也是 iostream 运行缓慢的主要原因之一。 ifstream
对象不一定附加到文件。 stringstream
对象可能附加到文件。大量的多态性,大量的虚拟调用,没有优化的机会。
但是无论您是否重新绑定(bind)流,您都会为这些缺点付出代价。
关于c++ - 从 ifstream 读取 stdin/cin 是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22004027/