理想情况下,我想在这个程序中使用一个堆栈,但它不允许我这样做。我不断收到段错误 11。我通过创建一个新向量并将其设置为等于第一个向量然后弹出新向量来解决这种问题。但我最终需要将弹出的向量与第一个向量同步,但它会引发段错误 11。
无论如何,这里是代码,它是另一个项目的一部分,但我认为上下文不太重要,无法知道它的用途。此外,这确实会在我运行此方法时编译它,它会引发错误。
void print_well_formed_file(ifstream& ifs) {
vector<string> vec;
vector<string> finalVec;
string line;
string a;
string temp;
string final;
Lexer lexer;
Token tok;
while (getline(ifs, line)) {
lexer.set_input(line);
while (lexer.has_more_token()) {
tok = lexer.next_token();
switch (tok.type) {
case TAG:
if (tok.value[0] != '/'){
a = (tok.value);
if (a == "red") {
a = "\033[31m";
}
if (a == "green") {
a = "\033[32m";
}
if (a == "blue") {
a = "\033[34m";
}
if (a == "yellow") {
a = "\033[33m";
}
if (a == "magenta") {
a = "\033[31m";
}
if (a == "cyan") {
a = "\033[36m";
}
if (a == "dim") {
a = "\033[2m";
}
if (a == "underline") {
a = "\033[4m";
}
if (a == "bright") {
a = "\033[1m";
}
vec.push_back(a);
final = temp + a;
}
else{
string s;
string temp;
string fin;
finalVec = vec;
if(!finalVec.empty()){finalVec.pop_back();}
a = "\033[0m" + finalVec.back();
temp = final;
final = temp + a;
if(!vec.empty()){vec.pop_back();} // throws an error
vec = finalVec; // throws an error
}
break;
case IDENT:
a= (tok.value)+ " ";
temp = final;
final = temp + a;
break;
}
}
}
cout << final << endl;
}