Tenshi Vess
Tenshi Vess

Reputation: 1

Vector Iterator not Incrementable In for loop

Alright so I know people have gone on and on about this topic, but I've searched question after question and edited my code repeatedly, and I still seem to be having this problem. This piece of code is basically intended to run through a vector and compare/combine its contents. In order to do that, I use 'it' and 'in' to go through the vector and .erase to delete contents that have been combined into new parts of the vector. Knowing I would basically need the .erase function and iterators, I used some code (the auto it stuff) from other questions on StackOverflow that seemed to work in this situation. Since I'm unfamiliar with that code, I may have used it incorrectly for this situation, though I'm not sure about that.

Anyway, as the title indicates, I'm getting a 'vector iterator not incrementable' error. The program seems to be running into this error after going through the second for loop a few times. I've tried a lot of things, but I just can't seem to figure out what part of my code is the real problem.

Any help would be greatly appreciated!

\if (!strvector.empty()) {
        for (auto in = strvector.begin(); in != strvector.end() - 1;) {//in order to process if there is more than one final piece

            str = ' ' + *in + ' ';//adds spaces so it only compares beginnings & ends
            if (strvector.size() < 2) {
                break;
            }//doesn't do anything if there are too few objects to process

            for (auto it = strvector.begin(); it != strvector.end() - 1;) { 
                if (strvector.size() < 2) {
                    break;
                }//doesn't continue if there are too few objects to process
                str2 = ' ' + *it + ' '; //adds spaces so it only compares beginnings & ends
                substr = str; //sets substring of string to be chopped up
                while (substr.length() >= 6) { //only processes substrings >= 6 characters bc smaller bits are useless
                    size_t found = str2.find(substr); //searches str2 for substr
                    if (found != string::npos) {
                        str = str.substr(0, substr.length()) + ' '; //cuts substr off of str
                        str.append(str2); //adds str and str2
                        it = strvector.erase(it);
                        substr = 'a'; //shortens substr to get out of while
                        test=1;
                    }//end if
                    else {
                        substr.erase(substr.size() - 1, 1); //if substr was not found, decrement substr and compare again
                    }
                }//end while

                substr = str; //resets substr
                while (substr.length() >= 6) { //only processes substrings >= 6 characters bc smaller bits are useless
                    size_t found = str2.find(substr); //searches str2 for substr
                    if (found != string::npos) {
                        str = str.substr(substr.length()) + ' '; //cuts substr from beginning of string
                        str = str2 + str; //adds str2 and str, with str2 at the beginning
                        it = strvector.erase(it++);
                        substr = 'a'; //shortens substr to get out of while 
                        test=1;

                    }//end if
                    else {
                        substr.erase(0, 1); //erases the first character of the string
                    }
                    if (test < 1) {
                        it++; //increments if the erase function did not already do that
                    }

                }//end while
                if (test != 1) {
                    it++; //increments if the erase function did not already do that
                }
                if (test < 2) {
                    strvector.push_back(str); //adds new str to the vector
                    test = 0;
                }
            }//end ptr2 for
            if (strvector.size() < 2) {
                in = strvector.erase(in - 1);
            }
            else {
                in++;
            }
            cout << "str1 is " << str << endl; //prints out str
        }//end ptr for
    }//end if vector is not empty

Upvotes: 0

Views: 1006

Answers (1)

Hawkmooon
Hawkmooon

Reputation: 508

Once you call erase on a std::vector the iterator that you're using becomes invalid. Instead the call to erase returns a brand new iterator that you should use going forward.

In your case, you're using the postfix ++ operator which will attempt to increment the iterator after it has been used by the erase method. At that point the iterator is invalid and so you get an error.

What you likely want is it = strvector.erase(it); which removes the element at the iterator and then returns a new iterator positioned at the element after the one you erased. You don't need the additional ++ because erase effectively did that for you.

Upvotes: 1

Related Questions