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


Groups > comp.lang.c++ > #10108 > unrolled thread

Defining iterator type through container object type

Started byUrs Thuermann <urs@isnogud.escape.de>
First post2011-09-14 23:51 +0200
Last post2011-11-04 15:10 +0100
Articles 7 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  Defining iterator type through container object type Urs Thuermann <urs@isnogud.escape.de> - 2011-09-14 23:51 +0200
    Re: Defining iterator type through container object type Ian Collins <ian-news@hotmail.com> - 2011-09-15 10:25 +1200
      Re: Defining iterator type through container object type Juha Nieminen <nospam@thanks.invalid> - 2011-09-15 06:49 +0000
        Re: Defining iterator type through container object type Marc <marc.glisse@gmail.com> - 2011-09-15 08:03 +0000
          Re: Defining iterator type through container object type Urs Thuermann <urs@isnogud.escape.de> - 2011-11-03 14:30 +0100
            Re: Defining iterator type through container object type Marc <marc.glisse@gmail.com> - 2011-11-03 18:17 +0000
              Re: Defining iterator type through container object type Urs Thuermann <urs@isnogud.escape.de> - 2011-11-04 15:10 +0100

#10108 — Defining iterator type through container object type

FromUrs Thuermann <urs@isnogud.escape.de>
Date2011-09-14 23:51 +0200
SubjectDefining iterator type through container object type
Message-ID<ygf7h5ayere.fsf@janus.isnogud.escape.de>
Can I define an iterator for an object of a standard container class
without using the concrete type of that container object?

I tried things like

    1 #include <list>
    2 void foo(std::list<int> ilist) {
    3     ilist.iterator it;
    4     for (it = ilist.begin(); it != ilist.end(); ++it) { *it; }
    5 }

and

    1 #include <list>
    2 void foo(std::list<int> ilist) {
    3     typeof(ilist)::iterator it;
    4     for (it = ilist.begin(); it != ilist.end(); ++it) { *it; }
    5 }

For the first code I get 

    foo.cc:3:11: error: invalid use of 'std::list<int>::iterator'

so at least ilist.iterator seems indeed to be understood as
std::list<int>::iterator.  In the second case I get

    foo.cc:3:29: error: expected initializer before 'it'

Is there a way to define the iterator without using std::list<int>::iterator
explicitly?

urs

[toc] | [next] | [standalone]


#10111

FromIan Collins <ian-news@hotmail.com>
Date2011-09-15 10:25 +1200
Message-ID<9dcnusF786U1@mid.individual.net>
In reply to#10108
On 09/15/11 09:51 AM, Urs Thuermann wrote:
> Can I define an iterator for an object of a standard container class
> without using the concrete type of that container object?
>
> I tried things like
>
>      1 #include<list>
>      2 void foo(std::list<int>  ilist) {
>      3     ilist.iterator it;
>      4     for (it = ilist.begin(); it != ilist.end(); ++it) { *it; }
>      5 }
>
> and
>
>      1 #include<list>
>      2 void foo(std::list<int>  ilist) {
>      3     typeof(ilist)::iterator it;
>      4     for (it = ilist.begin(); it != ilist.end(); ++it) { *it; }
>      5 }
>
> For the first code I get
>
>      foo.cc:3:11: error: invalid use of 'std::list<int>::iterator'
>
> so at least ilist.iterator seems indeed to be understood as
> std::list<int>::iterator.  In the second case I get
>
>      foo.cc:3:29: error: expected initializer before 'it'
>
> Is there a way to define the iterator without using std::list<int>::iterator
> explicitly?

Only if your compiler supports the new auto keyword.  Otherwise use a 
typedef for the parameter type.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#10126

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-09-15 06:49 +0000
Message-ID<4e719ffc$0$4353$7b1e8fa0@news.nbl.fi>
In reply to#10111
Ian Collins <ian-news@hotmail.com> wrote:
> Only if your compiler supports the new auto keyword.  Otherwise use a 
> typedef for the parameter type.

  Would decltype(container)::iterator work in C++0x?

[toc] | [prev] | [next] | [standalone]


#10134

FromMarc <marc.glisse@gmail.com>
Date2011-09-15 08:03 +0000
Message-ID<j4sbg9$a8a$2@news-rocq.inria.fr>
In reply to#10126
Juha Nieminen  wrote:

> Ian Collins <ian-news@hotmail.com> wrote:
>> Only if your compiler supports the new auto keyword.  Otherwise use a 
>> typedef for the parameter type.
> 
>   Would decltype(container)::iterator work in C++0x?

C++11 now. Yes, but it would be easier to just write:
auto it=ilist.begin();

[toc] | [prev] | [next] | [standalone]


#11233

FromUrs Thuermann <urs@isnogud.escape.de>
Date2011-11-03 14:30 +0100
Message-ID<ygffwi5z624.fsf@janus.isnogud.escape.de>
In reply to#10134
Marc <marc.glisse@gmail.com> writes:

> Juha Nieminen  wrote:
> 
> > Ian Collins <ian-news@hotmail.com> wrote:
> >> Only if your compiler supports the new auto keyword.  Otherwise use a 
> >> typedef for the parameter type.
> > 
> >   Would decltype(container)::iterator work in C++0x?
> 
> C++11 now. Yes, but it would be easier to just write:
> auto it=ilist.begin();

Ok, that looks nice and GCC already supports it.  Is it also possible
to define a const_iterator from a non-const container object using the
auto keyword, i.e. a list<T>::const_iterator from an object of type
list<T>?

urs

[toc] | [prev] | [next] | [standalone]


#11246

FromMarc <marc.glisse@gmail.com>
Date2011-11-03 18:17 +0000
Message-ID<j8ulsi$sed$1@news-rocq.inria.fr>
In reply to#11233
Urs Thuermann  wrote:

> Marc <marc.glisse@gmail.com> writes:
> 
>> Juha Nieminen  wrote:
>> 
>> > Ian Collins <ian-news@hotmail.com> wrote:
>> >> Only if your compiler supports the new auto keyword.  Otherwise use a 
>> >> typedef for the parameter type.
>> > 
>> >   Would decltype(container)::iterator work in C++0x?
>> 
>> C++11 now. Yes, but it would be easier to just write:
>> auto it=ilist.begin();
> 
> Ok, that looks nice and GCC already supports it.  Is it also possible
> to define a const_iterator from a non-const container object using the
> auto keyword, i.e. a list<T>::const_iterator from an object of type
> list<T>?

Yes, call cbegin instead of begin ('c' for "const").

[toc] | [prev] | [next] | [standalone]


#11254

FromUrs Thuermann <urs@isnogud.escape.de>
Date2011-11-04 15:10 +0100
Message-ID<ygfvcr0m11e.fsf@janus.isnogud.escape.de>
In reply to#11246
Marc <marc.glisse@gmail.com> writes:

> > Ok, that looks nice and GCC already supports it.  Is it also possible
> > to define a const_iterator from a non-const container object using the
> > auto keyword, i.e. a list<T>::const_iterator from an object of type
> > list<T>?
> 
> Yes, call cbegin instead of begin ('c' for "const").

Ah, thanks.  Haven't seen that before.  Now I changed some of the code
I currently work on and it looks so much cleaner.  Thanks again.

urs

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web