The above code contains several errors, though, which makes the results undefined:
Code:
bool is_line_valid()
{
//Return true if there is a new line character after the last #
list<char>::iterator list_iter = row_chars.end();
while(*list_iter != '#') //dereferences row_chars.end() right away; what happens if there is no '#'?
{
list_iter--;
}
cout << "Finished while loop; print what is at current container: " << *list_iter << "\n";
cout << "Now move container forward (to last position)... \n";
list_iter++;
if(*list_iter == '\n') //what if '#' was the last character?
return true;
else
return false;
}
You could rather make use of the standard algorithms, for example:
Code:
bool is_line_valid()
{
std::list<char>::const_reverse_iterator it = std::find(row_chars.rbegin(), row_chars.rend(), '#');
if (it != row_chars.rend()) //found
{
std::list<char>::const_iterator next = it.base();
return next != row_chars.end() && *next == 'n'; //if next character exists and is line-break
}
return false;
}