次のコードスニペットは、stringstreamオブジェクトを使用して文字列から整数を抽出し、整数抽出が成功したかどうかを検出することを目的としています。stringstreamクラスは、>>演算子を継承して、istreamインスタンスへの参照を返します。整数抽出に失敗すると、strメンバーがまだstrInputであるのに、myStreamが0に等しくなるのはどうしてですか?
stringstream myStream(strInput);
if (myStream >> num){//successfull integer extraction}
else{//unsuccessfull integer extraction
cout<<myStream<<endl;
cout<<myStream.str().c_str()<<endl;}