I'm about to kill myself... This is very elementary stuff, yet I still can't do it.
Let me first give you the code and then explain my problem :
wordListIterator = wordList.begin();
list<string>::iterator wordListNextIterator = wordList.begin();
wordListNextIterator++;
while(wordListIterator != wordList.end())
{
/* Counting duplicates */
int count = 1;
while(*wordListIterator == *wordListNextIterator)
{
count++;
wordListNextIterator = wordList.erase(wordListNextIterator);
if(wordListNextIterator != wordList.end())
{
wordListNextIterator = wordListIterator;
wordListNextIterator++;
}
}
/* Checking whether the word is a palindrome */
int z = 0;
int length = (*wordListIterator).length();
int palindrome = 0;
char currentWordReverse[100] = "";
strcpy(currentWord, (*wordListIterator).c_str());
currentWord[0] += 32; // Palindrom kontrolü için ilk harfi küçük harfe çeviriyoruz
for(z = 0; z < length; z++)
{
currentWordReverse[z] = currentWord[length - z - 1];
}
currentWordReverse[length] = '\0';
if(*currentWord == *currentWordReverse)
{
palindrome = 1;
}
currentWord[0] -= 32; // Kelimenin ilk harfini tekrar büyük harfe çeviriyoruz
/* Displaying the result*/
std::cout << currentWord;
if(palindrome == 1)
{
std::cout << " (P)";
}
std::cout << " : " << count << "\n";
wordListIterator++;
if(wordListIterator != wordList.end())
{
wordListNextIterator = wordListIterator;
wordListNextIterator++;
}
}
I have a string list with two iterators, the second is the successor of the first.
The problem is caused by the code block shown in red. When I run the program, it processes the list until the last element, after which the program crashes. I know the problem arises from wordListNextIterator overflowing beyond wordList.end() However, no matter how many checks and controls I try, I cannot prevent it from happening and it's driving me crazy.
If I start the main while block as while(wordListNextIterator != wordList.end()) there are no problems, but naturally the last item in the list is not processed.
Any help will be appreciated...
Thanks in advance...