Thursday, 5 September 2013

Vector iterator not dereferencable in for loop

Vector iterator not dereferencable in for loop

I'm using a loop to count how many times that a word was entered then
print the word and how many times it was entered, which works but it never
prints the last word, I have it sorted alphabetically. Before the last
word is printed it errors out saying the iterator is not dereferencable.
Here is my code for the loop:
for (vector<string>::iterator it = v.begin() ; it != v.end(); ++it)
{
if (*it == *(it+1))
{
count++;
}
else if (*it != *(it+1))
{
count++;
cout << *it << " ---- " << count << endl;
count=0;
}
}

No comments:

Post a Comment