Defining iterator type through container object type

U

Urs Thuermann

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
 
I

Ian Collins

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.
 
J

Juha Nieminen

Ian Collins said:
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?
 
U

Urs Thuermann

Marc said:
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
 
M

Marc

Urs said:
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").
 
U

Urs Thuermann

Marc said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top