Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • You really should mention the (IMHO better) option of using a middle-tested loop. Commented Aug 16, 2010 at 22:42
  • You don't need the else clause on your if check, since the true branch breaks your control logic out of the loop anyway. I would have written it more like if (iter == keywords.end()) break;. Also. if you are going to increment something every loop iteration, I find it easier to read if you go ahead and use a for loop and put the iteration in the iteration slot there where people are used to seeing it and know exactly what you are doing. Commented Jun 8, 2011 at 13:40
  • 1
    ...of course, when done with all that cleanup, you get essentially my answer below. It does this with five lines of text (six if I add the empty() check properly) where this takes up 17. I think it is much easier to understand too. It puts the iteration in a standard place, and gets rid of two entire levels of nesting around the comma-producing code. Commented Jun 8, 2011 at 13:52
  • @T.E.D. The reason I didn't use a for loop and put the increment in the third statement is because that won't work: I need to print the item and then increment before doing the end test. Commented Aug 8, 2011 at 20:33
  • That's why you put the end test in the middle of the loop. See my answer below. Commented Aug 9, 2011 at 12:46