help on std::iterator_traits?



 DEVELOP > c-Plus-Plus > help on std::iterator_traits?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Jess"
Date: 28 Jun 2007 09:30:55 AM
Object: help on std::iterator_traits?
Hello,
I read a document that says iterator_traits is contains only nested
definitions. It seems to have definitions like value_type, reference
etc. If I define my own container, I can define these types without
iterator_traits. Therefore, could somebody please tell me why we need
to use iterator_traits at all?
Thanks a lot!
Jess
.

User: "Kai-Uwe Bux"

Title: Re: help on std::iterator_traits? 28 Jun 2007 09:58:05 AM
Jess wrote:

Hello,

I read a document that says iterator_traits is contains only nested
definitions. It seems to have definitions like value_type, reference
etc. If I define my own container,

You probably mean "iterator" instead of "container"

I can define these types without
iterator_traits. Therefore, could somebody please tell me why we need
to use iterator_traits at all?

The iterator_traits are the interface for algorithms to know about the
iterators passed. Note that T* is supposed to be a valid iterator. Yet,
there is no such thing as (T*)::value_type. However, iterator_traits<T*> is
specialized in the expected way. This is another case of a problem being
solved by introducing "yet another level of indirection"(tm).
Best
Kai-Uwe Bux
.

User: "Zeppe"

Title: Re: help on std::iterator_traits? 28 Jun 2007 10:01:12 AM
Jess wrote:

Hello,

I read a document that says iterator_traits is contains only nested
definitions. It seems to have definitions like value_type, reference
etc. If I define my own container, I can define these types without
iterator_traits. Therefore, could somebody please tell me why we need
to use iterator_traits at all?

Because the algorithms that rely on the iterators don't really care
about your container. If you write a container, and write your own
iterator for it, an algorithm that performs some operation on your data
can be written as:
template <class It>
void foo(It begin, It end){
iterator_traits<It>::value_type v;
// bla bla bla...
}
it's an elegant way to group all the information that depends on the
iterator.
Regards,
Zeppe
.

User: "Victor Bazarov"

Title: Re: help on std::iterator_traits? 28 Jun 2007 09:55:30 AM
Jess wrote:

I read a document that says iterator_traits is contains only nested
definitions. It seems to have definitions like value_type, reference
etc. If I define my own container, I can define these types without
iterator_traits. Therefore, could somebody please tell me why we need
to use iterator_traits at all?

You don't.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER