【发布时间】:2014-10-21 04:19:06
【问题描述】:
该函数 buildTree 读取文本输入(包含在名为 argv[1] 的文件中)。然后,我打开文件,逐个字符读取,如果有新行 ("if (token == '\n')") 跟踪此行号并将其存储在向量中以便以后访问它。接下来,它将它分解为一系列单词(使用除数字或字母符号之外的任何字符作为终止符)。这是我遇到错误的地方。然后我尝试将每个字符添加到字符串中,然后当标记是数字或字母符号时,然后将字符串推送到向量中,以便我以后可以访问它。我的逻辑对吗?在将每个单词推入向量时,您能否帮助解决我的错误。
如有混淆,请见谅
BinarySearchTree buildTree (char *argv[]){
ifstream file;
vector<char *> V;
int line = 0;
vector<int> LineNumber;
file.open(argv[1],ios::in);
char token;
string word[] = {};
if (file.is_open()){
token = file.get();//reads the next character from a stream
if (token == '\n')
line++;
LineNumber.push_back(line);
while (token!= ' ' || '0' || '1' || '2' || '3' || '4' || '5' ||'6' || '7' || '8' || '9'){
//while character is not space, digit, or non-alphabetic character
word += token;//adds character to string array *error here
}
V.push_back(word);//adds word to vector *error here
}
}
【问题讨论】:
-
while条件检查不会按照您的想法进行。
标签: c++ string vector character argv