Uva 536 关于输入的判定


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<sstream>
#include<istream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 50;
int pre[maxn], ino[maxn];
int lch[maxn], rch[maxn];
int n;
bool read(int* a) {
    n = 0;
    string line;
    **if (!(cin >> line))return false;**
    stringstream ss(line);
    char x = 0;
    while (ss >> x)a[n++] = (int)x-64;
    while (n > 0)return true;/*检测末尾*/
}
int build(int L1, int R1, int L2, int R2) {
    if (L2 > R2)return 0;
    int root = pre[L1];
    int p=L2;
    while (ino[p] != root)p++;/*cnt统计左子树的个数,此处少一*/
    int cnt = p - L2;
    lch[root] = build(L1 +1, L1 + cnt, L2, p - 1);/*范围按各个遍历的顺序来决定*/
    rch[root] = build(L1 + cnt+1, R1, p + 1, R2);
    return root;
}
void dfs(int root) {
    if (lch[root])dfs(lch[root]);
    if (rch[root])dfs(rch[root]);
    printf("%c", root + 64);
}
int main() {
    while (read(pre)) {
        read(ino);
        build(0, n - 1, 0, n - 1);
        dfs(pre[0]);
        printf("\n");
    }
    return 0;
}

如上有两个星号包围的语句,如果我不加以if进行判定的话,本来ac的代码会runtime error,这是怎么一个情况?

该回答引用GPTᴼᴾᴱᴺᴬᴵ
这是因为如果输入流(cin)遇到文件结束符或输入错误,就会设置一个错误标志,之后对输入流的操作将失败,直到清除该错误标志。当发生这种错误时,cin 会将标志位置为 true,而输入操作返回 false。因此,当输入读取失败时,代码需要进行错误处理,以避免对后续操作产生影响。在这里,你使用了一个 stringstream 对象从输入流中读取了数据,所以要检查 stringstream 对象读取是否成功。