Defining iterator type through container object type

Discussion in 'C++' started by Urs Thuermann, Sep 14, 2011.

  1. 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
    Urs Thuermann, Sep 14, 2011
    #1
    1. Advertising

  2. Urs Thuermann

    Ian Collins Guest

    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
    Ian Collins, Sep 14, 2011
    #2
    1. Advertising

  3. Ian Collins <> 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?
    Juha Nieminen, Sep 15, 2011
    #3
  4. Urs Thuermann

    Marc Guest

    Juha Nieminen wrote:

    > Ian Collins <> 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();
    Marc, Sep 15, 2011
    #4
  5. Marc <> writes:

    > Juha Nieminen wrote:
    >
    > > Ian Collins <> 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
    Urs Thuermann, Nov 3, 2011
    #5
  6. Urs Thuermann

    Marc Guest

    Urs Thuermann wrote:

    > Marc <> writes:
    >
    >> Juha Nieminen wrote:
    >>
    >> > Ian Collins <> 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").
    Marc, Nov 3, 2011
    #6
  7. Marc <> 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
    Urs Thuermann, Nov 4, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivi Orunitia
    Replies:
    11
    Views:
    4,451
    Martijn Lievaart
    Feb 4, 2004
  2. johny smith
    Replies:
    8
    Views:
    403
    Peter Koch Larsen
    Jul 2, 2004
  3. michaelmei
    Replies:
    2
    Views:
    689
    michaelmei
    Oct 9, 2006
  4. Generic Usenet Account
    Replies:
    2
    Views:
    303
    Andre Kostur
    Oct 6, 2007
  5. yatko
    Replies:
    5
    Views:
    408
    James Kanze
    Jan 23, 2008
Loading...

Share This Page