アクセラレートされた c++の例を次に示します。
// read homework grades from an input stream into a `vector<double>'
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
// get rid of previous contents
hw.clear();
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}
行は、in.clear()
以前の入力によって生成された可能性のあるエラーをクリアすることになっていますが、なぜ入力を開始する前、つまりwhile (in >> x)
行の前にクリアされないのですか? 実際、入力ストリームを 2 回 (hw に入力する前と後に) クリアする方が安全ではないでしょうか?