using stringstream >> operator in if statement
下面的代码段用于尝试使用Stringstream对象从字符串中提取整数,并检测整数提取是否成功。Stringstream类继承>>运算符以返回对IStream实例的引用。整数提取失败如何导致mystream在str成员仍为strinput时等于0?
1 2 3 4 5 | stringstream myStream(strInput); if (myStream >> num){//successfull integer extraction} else{//unsuccessfull integer extraction cout<<myStream<<endl; cout<<myStream.str().c_str()<<endl;} |
有在
答案是在运营商是最
A stream object derived from ios can be casted to a pointer. This pointer is a null pointer if either one of the error flags (failbit or badbit) is set, otherwise it is a non-zero pointer.
当你得到这个操作符被称为流是在
1 2 3 | if (!(myStream >> num)) { ... } |