如何在不从 cin 中提取下一行的情况下检查下一行的长度?
我有以下形式的输入数据:
A
-B
--*
--C
-D
--E
---*
---F
0
我有一个递归函数可以从中构建一棵树。
TreeNode* BuildTree() {
string line;
getline(cin, line);
char id = line[line.size() - 1];
if (id == '0') return NULL;
TreeNode* n = (id == '*') ? NULL : new TreeNode(id, NULL, NULL);
if (/* This line's size less than next line's size */) {
n->left = BuildTree();
n->right = BuildTree();
}
return n;
}
最佳答案
编写您的代码,使您领先一行。然后你的 if 语句变成:
if (/* this line's size is greater than the previous line's size */) {
....
}
或者,如果您能负担得起内存(很可能您能负担得起),则将整个文件放入 vector<string>
中。然后在那个上递归。
关于c++ - 从 cin 中选择下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20321247/