C++ 一个特定的测试用例无论如何都不会通过测试

C++ 一个特定的测试用例无论如何都不会通过测试,c++,string,vector,token,erase,C++,String,Vector,Token,Erase,代码的目的是从根本上删除文本文件中存在的无用数组中的单词。我遇到了一个非常奇怪的问题,代码不会删除短语“waitingontheshelf”中的“the”,但是每隔一个测试用例(很多)就通过了。有什么想法吗 int main(){ string useless[20] = { "an", "the" , "of", "to", "and", "but", "nor", "or", "some", "any", "very", "in", "on", "at", "before", "af

代码的目的是从根本上删除文本文件中存在的无用数组中的单词。我遇到了一个非常奇怪的问题,代码不会删除短语“waitingontheshelf”中的“the”,但是每隔一个测试用例(很多)就通过了。有什么想法吗

int main(){
    string useless[20] = { "an", "the" , "of", "to", "and", "but", "nor", "or", "some", "any", "very", "in", "on", "at", "before", "after", "into", "over", "through", "along"};

    ifstream fin("input.txt");
    if(fin.fail()){
        cout << "Input failed to open" << endl;
        exit(-1);
    }

    string line;
    getline(fin, line);
    getline(fin, line);
    getline(fin, line);
    getline(fin, line);

    ofstream fout("output.txt");

    while(getline(fin, line)){
        vector<string> vec;
        istringstream iss(line);
        while (iss) {
            string word;
            iss >> word;
            transform(word.begin(), word.end(), word.begin(), ::tolower);
            vec.push_back(word);
        }

        for(int i = 0; i < vec.size(); i++){
            for(int j = 0; j < 20; j++){
                if(vec[i] == useless[j]){
                    vec.erase(remove(vec.begin(), vec.end(), vec[i]), vec.end());
                }
            }
            fout << vec[i] << " ";
        }
        fout << endl;
    }
}
intmain(){
字符串无用[20]={“an”,“the”,“of”,“to”,“and”,“but”,“nor”,“or”,“some”,“any”,“very”,“in”,“on”,“at”,“before”,“after”,“into”,“over”,“through”,“along”};
ifstream fin(“input.txt”);
if(fin.fail()){
单词;
转换(word.begin(),word.end(),word.begin(),::tolower);
向量推回(word);
}
对于(int i=0;ifout您在这里使用了不正确的迭代

 for(int i = 0; i < vec.size(); i++){
        for(int j = 0; j < 20; j++){
            if(vec[i] == useless[j]){
                vec.erase(remove(vec.begin(), vec.end(), vec[i]), vec.end());
            }
        }
        fout << vec[i] << " ";
    }
    fout << endl;
}
之后,您将得到过滤向量,在无用的数组中没有单词

顺便说一句,我们可以对其进行优化。上面的算法具有下一个复杂性:O(vec_大小*无用的大小)。我们可以将其优化为O(vec_大小)。 可以使用散列集合(无序集合)代替数组,它为元素访问提供了恒定的时间

 unordered_set<string> useless = { "an", "the" , "of", "to", "and", "but", "nor", "or", "some", "any", "very", "in", "on", "at", "before", "after", "into", "over", "through", "along" };
 ...
 vec.erase(remove_if(vec.begin(), vec.end(), [&](const string& str)
 {
     return  useless.find(str) != useless.end();
 }), vec.end());
unordered\u set无用={“an”、“the”、“of”、“to”、“and”、“but”、“nor”、“or”、“some”、“any”、“very”、“in”、“on”、“at”、“before”、“after”、“into”、“over”、“through”、“along”};
...
向量擦除(如果(向量开始(),向量结束(),[&](常量字符串和字符串)删除_)
{
返回无用。查找(str)!=无用。结束();
}),vec.end());

欢迎使用Stack Overflow!听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看程序偏离预期的地方。如果您要进行任何编程,这是一个必不可少的工具。进一步阅读:。是的,是的,我可以看到如何进行去毛刺gger真的会帮我。我目前没有使用任何IDE(只是升华和终端)因此缺少调试器。使用调试器不需要IDE,只需从命令行使用gdb即可。通过将
main
中的内容拆分为两个或三个函数,为代码添加更多结构也可能是一个好主意。因此,解决方案是在每次删除元素时重置迭代器?编辑:或直接删除记得每次删除一个元素时我吗?我会在答案中写下解决方案。你可以为std::remove_if方法写下你自己的条件。对于vec中的每个元素,我尝试使用std::find方法在无用集合中查找元素。如果我找到它(std::find的结果不等于数组的结尾),我返回true(对于std::remove_,如果谓词的意思是:delete元素)。
 unordered_set<string> useless = { "an", "the" , "of", "to", "and", "but", "nor", "or", "some", "any", "very", "in", "on", "at", "before", "after", "into", "over", "through", "along" };
 ...
 vec.erase(remove_if(vec.begin(), vec.end(), [&](const string& str)
 {
     return  useless.find(str) != useless.end();
 }), vec.end());