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


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

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

From Nikos Chantziaras <realnc@arcor.de>
Newsgroups comp.lang.c++
Subject Re: Initializing iterators to one past end() in for loops
Date 2011-04-14 21:45 +0300
Organization Lucas Barks
Message-ID <io7fbq$fua$1@news.grnet.gr> (permalink)
References <io554k$9hn$1@news.grnet.gr> <io56qc$cu8$1@news.grnet.gr> <Xns9EC7481B07908myfirstnameosapriee@216.196.109.131>

Show all headers | View raw


On 04/14/2011 07:05 AM, 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.
> [...]
>
> if (!lst.empty()) {
>      for (it_t i = std::next(lst.begin()); i != lst.end(); ++i) {
>          // do something
>      }
> }

Yep, that looks nicer!  It's C++0x, but seems to be supported well 
enough by now.

Back to comp.lang.c++ | Previous | Next — Previous 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