iterators

N

nsgi_2004

Hello, I made a table class (implemented with an array) and would like to
add iterators so that I can use the std algorithms with it.

I added this to my class:

typedef T* iterator;
iterator begin();
iterator end();

Is that all I need to do?
 
V

Victor Bazarov

nsgi_2004 said:
Hello, I made a table class (implemented with an array) and would like to
add iterators so that I can use the std algorithms with it.

I added this to my class:

typedef T* iterator;
iterator begin();
iterator end();

Is that all I need to do?

Basically, a pointer to object adheres to the rules set for random-access
iterators, so if your 'iterator' is a pointer, then yes, it's probably all
you need. Why do you ask? Have you encountered any problems?

V
 
J

John Harrison

Hello, I made a table class (implemented with an array) and would like to
add iterators so that I can use the std algorithms with it.

I added this to my class:

typedef T* iterator;
iterator begin();
iterator end();

Is that all I need to do?

That will work since pointers are a kind of iterator. I would also add the
following

typedef const T* const_iterator;
const_iterator begin() const;
const_iterator end() const;

john
 
N

nsgi_2004

Victor Bazarov said:
Basically, a pointer to object adheres to the rules set for random-access
iterators, so if your 'iterator' is a pointer, then yes, it's probably all
you need. Why do you ask? Have you encountered any problems?

Well, I just sort of "found out" about the std algorithms, functors, and
such. So I just wanted to make sure that, even though it was working, I
wasn't doing anything that went against the STL design as I begin to use the
STL for more than just the containers.
 

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,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top