Line Justification

Pages: 12
Great thank you both for helping me better understand this.
now with line 38 pos = s.find_first_of(' ', pos + 2);
why would it be pos+2 instaed of 1?
Good question. Initially I had written it as +1 itself but then it went to an infinite loop. Then I noticed that after the position was found, I added a whitespace. So the actual length of the string increases by one and hence we have to start searching from +2 instead of +1 because +1 will return us back to the last found position.

pos+1 would work if you inserted whitespace AFTER finding position, but you would have to make sure that pos is not set to npos (when no match was found) by using another conditional.

Both have the same output but the first approach doesn't need the conditional ^_^

Thank you!
Topic archived. No new replies allowed.
Pages: 12