在 this question接受答案的作者不推荐使用输入运算符,因为:
operator>>
is subject toiomanip
stream manipulators and other "funny" stuff, so you can never be sure that it does what's advertised.
但这意味着什么?为什么我们在用 C++ 编程时要避免使用 C++ 的输入运算符?
我从 The Definitive C++ Book Guide and List 读过的书没有提到这一点,他们引入并使用了输入运算符。我也没有在互联网上找到任何关于这个主题的有用信息。
谢谢!
p.s:很抱歉,我没有足够的声望在该主题中提出我的问题。
最佳答案
我唯一能想到的是 operator>> 在使用之间保留了很多设置。例如:
std::ifstream fin("input.txt");
std::string str;
double num;
someOtherFunction(fin);
while (fin >> str >> num)
{
//do something
}
除非我们考虑,否则看起来很无辜:
void someOtherFunction(std::ifstream& fin){
fin.width(1);
fin.setf(/* some flags here */);
//...
}
我亲自阅读了原始文件并使用正则表达式进行解析,但我绝不是该主题的专家,因此应该谨慎对待建议。
关于c++ - 为什么要避免 C++ 中的输入运算符(operator>>)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21155395/