Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #37103
| From | Bo Persson <bop@gmb.dk> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: auto i:v as iterator |
| Date | 2015-07-16 11:09 +0200 |
| Message-ID | <d0pamvFh77dU1@mid.individual.net> (permalink) |
| References | <20150716103906.1f3f26b3@delligoertz.goertz.local> |
On 2015-07-16 10:39, Ralf Goertz wrote:
> Hi,
>
> is it possible to have the new auto syntax for loops and get an iterator
> instead of a copy or a reference to the stored object itself? I would
> love to use the new syntax, but often I need to know what number in a
> vector the current object is. The following program doesn't compile.
>
> #include <vector>
> #include <iostream>
>
> int main() {
> std::vector<int> v={47,11};
> for (auto i:v) {
> //std::cout<<i<<std::endl;
> std::cout<<distance(i,v.begin())<<endl;
> }
> return 0;
> }
>
>
If you have a reference to the element (auto&...), you can compute the
difference between &i and &v[0].
Bo Persson
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
auto i:v as iterator Ralf Goertz <me@myprovider.invalid> - 2015-07-16 10:39 +0200
Re: auto i:v as iterator Bo Persson <bop@gmb.dk> - 2015-07-16 11:09 +0200
Re: auto i:v as iterator Ralf Goertz <me@myprovider.invalid> - 2015-07-16 12:00 +0200
Re: auto i:v as iterator Öö Tiib <ootiib@hot.ee> - 2015-07-16 05:17 -0700
Re: auto i:v as iterator Ralf Goertz <me@myprovider.invalid> - 2015-07-16 15:47 +0200
Re: auto i:v as iterator Öö Tiib <ootiib@hot.ee> - 2015-07-16 08:27 -0700
Re: auto i:v as iterator Ralf Goertz <me@myprovider.invalid> - 2015-07-17 07:57 +0200
Re: auto i:v as iterator bartekltg <bartekltg@gmail.com> - 2015-07-16 18:06 +0200
Re: auto i:v as iterator "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2015-07-17 23:18 +0200
csiph-web