0

Just ran into this error, "Vector iterator not incrementable", while trying to populate a char vector using a for loop. The loop is supposed to check the first vector for where each correct letter is indexed then it is too populate the second vector with the letter at the same index accordingly. Is something in the loop causing this error?

    for (filler = word.begin(); filler != word.end(); filler++)
    {
        if (*filler == letter)
            guess.insert(filler, letter);
    }
László Papp
  • 51,870
  • 39
  • 111
  • 135

1 Answers1

0

filler is an iterator into the vector "word." It cannot be used as the first argument of an insert into guess, because filter does not point into guess.

If you want it to appear at the same index, you could reconstruct the index with filler - word.begin(). You could then use that to index into guess

for (filler = word.begin(); filler != word.end(); filler++)
{
    if (*filler == letter)
        guess.insert(guess.begin() + (filler - word.begin()), letter);
}
Cort Ammon
  • 10,221
  • 31
  • 45