带有向量和字符串的C ++ Segmentation Fault

时间:2014-02-22 04:17:58

标签: c++ string vector segmentation-fault

我的代码的一部分出现了分段错误,我查看并修复了我看过的大部分错误,但是我找不到所有的分段错误。

这段代码只是检查向量的标记中是否有单引号,并将单引号字符串组合成一个标记。

void file_parser::check_quote(vector<string> tokens)
{
    bool has_quote = false;
    string tmpstr;
    unsigned int FirstQuote = 0;
    unsigned int SecondQuote;
    string tmp;
    string tmpstr2;
    for (unsigned int i = 0; i <= tokens.size() - 1; i++) {
        tmp = tokens[i].c_str();
        for (unsigned int m = 0; m <= tmp.size() - 1; m++) {
            if (tmp[m] == '\'') {
                has_quote = true;
                FirstQuote = m;
            }
            if (has_quote && m != FirstQuote && tmp[m] == '\'') {
                SecondQuote = m;
                for (unsigned int k = FirstQuote; k <= SecondQuote; k++) {
                    tmpstr += tokens[k];
                }
                tokens.insert(tokens.begin() + FirstQuote, tmpstr);
                for (unsigned int j = FirstQuote + 1; j <= tokens.size() - 1; j++) {
                    for (unsigned int l = SecondQuote + 1; l <= tokens.size() - 1; l++) {
                        tmpstr2 = tokens[SecondQuote + 1];
                        tokens.insert(tokens.begin() + j, tmpstr2);
                    }
                }
                int test = tokens.size() - SecondQuote + 1;
                while (test > 0) {
                    tokens.erase(tokens.end() - 1);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题在于你的for循环中的k。您想要从tmp,引用(和包括)之间复制字符,但是您停止引用tmp,然后返回到令牌。