From 2811294413828500465
X-Google-Thread: 7894ca11fe,d47b345505a72a70
X-Google-Attributes: gid7894ca11fe,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news2.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!news.alt.net!frodo.cs.rpi.edu!not-for-mail
From: "Joe Smith" <unknown_kev_cat@hotmail.com>
Newsgroups: comp.std.c++
Subject: Re: for-else: adding else to for loops
Date: Wed, 19 Aug 2009 23:12:24 CST
Organization: Mixmin
Lines: 33
Sender: cppmods@cs.rpi.edu
Approved: austern@google.com
Message-ID: <h6ha3o$jcb$1@news.mixmin.net>
References: <5d82cd6c-2a22-4dd7-8c7d-790b579533ea@d15g2000prc.googlegroups.com>
NNTP-Posting-Host: netlab.cs.rpi.edu
Content-Type: text/plain;
	format=flowed;
	charset="iso-8859-1";
	reply-type=original
To: (Usenet)
Return-Path: <cppmods@ruralroute.cs.rpi.edu>
X-Original-Date: Wed, 19 Aug 2009 12:43:27 -0400
X-Submission-Address: std-c++@netlab.cs.rpi.edu
Xref: g2news2.google.com comp.std.c++:1310


"Jared Grubb" <jared.grubb@gmail.com> wrote:
> 3) by encapsulating the for-loop in a separate function (the "break"
> becomes "return" and the extra code follows the for loop).
>
> The third solution is probably the best way
> to do it, but sometimes encapsulating the for-loop out is not easy or
> nice.
>

With the ability to have (logically) nested functions due to lambdas

Could you not just do

auto iter=lst.begin();
[&]->void {
for (; iter != lst.end(); ++iter)
{
     if (*iter == NEEDLE) return;
}
cout << "needle not in the list";
}();

right where you would have otherwise put the code?



-- 
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



