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.

10
  • 1
    In general though, say for an object that doesn't have .empty(), is there a more concise way to for-else? Commented Aug 8, 2014 at 17:01
  • 2
    if (map.begin() == map.end()) Commented Aug 8, 2014 at 17:01
  • 1
    template<class R>bool empty(R const& r){using std::begin; using std::end; return begin(r)==end(r);} is a .empty() that works on all iterable ranges. Commented Aug 8, 2014 at 17:13
  • 3
    Is this else for-condensed-into-one-line actually familiar to people? I mean sure it's clever and accomplishes its purpose, but I can't help but wonder if it's recognized enough to be as readable as an indented for on the next line. Commented Aug 8, 2014 at 17:23
  • 2
    This isn't the same as a Python else since that executes if the container is empty, or it has been traversed through entirely. The only time it doesn't execute is on a break statement. See: docs.python.org/3.4/tutorial/… Commented Aug 8, 2014 at 18:47