Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Defining iterator type through container object type

Reply
Thread Tools

Defining iterator type through container object type

 
 
Urs Thuermann
Guest
Posts: n/a
 
      09-14-2011
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-14-2011
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
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      09-15-2011
Ian Collins <ian-> 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?
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      09-15-2011
Juha Nieminen wrote:

> Ian Collins <ian-> 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();
 
Reply With Quote
 
Urs Thuermann
Guest
Posts: n/a
 
      11-03-2011
Marc <> writes:

> Juha Nieminen wrote:
>
> > Ian Collins <ian-> 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
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      11-03-2011
Urs Thuermann wrote:

> Marc <> writes:
>
>> Juha Nieminen wrote:
>>
>> > Ian Collins <ian-> 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").
 
Reply With Quote
 
Urs Thuermann
Guest
Posts: n/a
 
      11-04-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem about defining an iterator yatko C++ 5 01-23-2008 12:36 PM
STL::Accessing the iterator for a "passed" container type Generic Usenet Account C++ 3 10-06-2007 11:35 AM
Error defining HashMap::iterator type, please help michaelmei C++ 2 10-09-2006 03:25 AM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments