Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #3638

Re: Initializing iterators to one past end() in for loops

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Initializing iterators to one past end() in for loops
Date Thu, 14 Apr 2011 16:20:43 +1200
Lines 34
Message-ID <90nb0sFeeiU14@mid.individual.net> (permalink)
References <io554k$9hn$1@news.grnet.gr> <io56qc$cu8$1@news.grnet.gr> <Xns9EC7481B07908myfirstnameosapriee@216.196.109.131>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net jBxofF8DpVA5M9fH+sY8oQNB9qjXNCKG8MB9Scf/ZOzR88hllJ
Cancel-Lock sha1:9V7YUfKZM0NXF85tS7aJiU0jXVQ=
User-Agent Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4
In-Reply-To <Xns9EC7481B07908myfirstnameosapriee@216.196.109.131>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:3638

Show key headers only | View raw


On 04/14/11 04:05 PM, Paavo Helde wrote:
> Nikos Chantziaras<realnc@arcor.de>  wrote in news:io56qc$cu8$1
> @news.grnet.gr:
>> Thanks for all the responses.  But I've made a mistake in my provided
>> example :-P  In the second loop, I'm using an std::list, not a vector:
>>
>>     std::list<int>  lst;
>>     typedef std::list<int>::iterator it_t;
>>     for (it_t i = lst.begin() + 1; i != lst.end(); ++i)
>>         // do something with *i
>>
>> (Converting some vectors to lists was the reason I came across this in
>> the first place.)
>>
>> I know it's not difficult to simply do something like:
>>
>>     it_t i = lst.begin();
>>     ++i;
>>     for ( ; i != lst.end(); ++i)
>>
>> but I was wondering whether there's some neat way to do that inside the
>> for header, so that 'i' will not stay in scope after the loop.
>
> std::list<int>  lst;
> typedef std::list<int>::iterator it_t;
> // populate list
>
> if (!lst.empty()) {
>      for (it_t i = std::next(lst.begin()); i != lst.end(); ++i) {

For those using C++0x compilers!

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Initializing iterators to one past end() in for loops Nikos Chantziaras <realnc@arcor.de> - 2011-04-14 00:38 +0300
  Re: Initializing iterators to one past end() in for loops Leigh Johnston <leigh@i42.co.uk> - 2011-04-13 22:46 +0100
  Re: Initializing iterators to one past end() in for loops Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-13 23:48 +0200
    Re: Initializing iterators to one past end() in for loops Leigh Johnston <leigh@i42.co.uk> - 2011-04-13 22:50 +0100
      Re: Initializing iterators to one past end() in for loops Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-14 00:23 +0200
        Re: Initializing iterators to one past end() in for loops Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-14 00:32 +0200
          Re: Initializing iterators to one past end() in for loops Noah Roberts <dont@email.me> - 2011-04-13 17:16 -0700
            Re: Initializing iterators to one past end() in for loops Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-14 05:57 +0200
              Re: Initializing iterators to one past end() in for loops Leigh Johnston <leigh@i42.co.uk> - 2011-04-14 14:29 +0100
                Re: Initializing iterators to one past end() in for loops Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-14 17:57 +0200
                Re: Initializing iterators to one past end() in for loops Leigh Johnston <leigh@i42.co.uk> - 2011-04-14 17:56 +0100
                Re: Initializing iterators to one past end() in for loops Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-14 19:33 +0200
              Re: Initializing iterators to one past end() in for loops Noah Roberts <dont@email.me> - 2011-04-14 09:54 -0700
                Re: Initializing iterators to one past end() in for loops Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-14 20:08 +0200
  Re: Initializing iterators to one past end() in for loops gwowen <gwowen@gmail.com> - 2011-04-13 14:49 -0700
  Re: Initializing iterators to one past end() in for loops Sam <sam@email-scan.com> - 2011-04-13 16:58 -0500
  Re: Initializing iterators to one past end() in for loops Nikos Chantziaras <realnc@arcor.de> - 2011-04-14 01:07 +0300
    Re: Initializing iterators to one past end() in for loops Ian Collins <ian-news@hotmail.com> - 2011-04-14 10:12 +1200
    Re: Initializing iterators to one past end() in for loops gwowen <gwowen@gmail.com> - 2011-04-13 15:24 -0700
    Re: Initializing iterators to one past end() in for loops Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-13 23:05 -0500
      Re: Initializing iterators to one past end() in for loops Ian Collins <ian-news@hotmail.com> - 2011-04-14 16:20 +1200
        Re: Initializing iterators to one past end() in for loops Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-13 23:52 -0500
        Re: Initializing iterators to one past end() in for loops Jeff Flinn <TriumphSprint2000@hotmail.com> - 2011-04-14 07:49 -0400
      Re: Initializing iterators to one past end() in for loops Nikos Chantziaras <realnc@arcor.de> - 2011-04-14 21:45 +0300

csiph-web