STL customer iterator

C

cai_xiaodong

I am very new to STL and generic programming. PLEASE help.
I have my own class looks like:

class pixelProperty {
public:
// get-functions here...
private:
Point mPosition;
int mCenterID;
Color mColor;
}

Basically the pixelProperty represents a 2D image data. To make it
more convenience for pixle random access, I would like to use the
Vector container in STL but with an "enhanced" iterator which give me
more convenience such as using a member function in the iterator,
myiterator::neighbor(x,y), which give me a 2d-style random access.

My questions are:
1) Can I acutally make my customer iterator by composition or inherit
from the vector::iterator (how about deriving from or composing the
vector container class - I read some posts in the forum but not fully
understand.... ) . if it is possible, could any one can be kind enough
to show me an example?

2) I would like to use a customer a random access iterator with the
STL algorithm (such as find()), take the pixelProperty as the data
type, how can I implement this STL-compatible iterator?

I really new to this area, any help will be greatly appreciated.
Thanks in advance.
Sheldon
 
F

flagos

(e-mail address removed) ha escrito:
I am very new to STL and generic programming. PLEASE help.
I have my own class looks like:

class pixelProperty {
public:
// get-functions here...


Cai:

Take a look at the book "The C++ Standard Library: A Tutorial and
Reference" by Nicolai M. Josuttis. There you will find an excellent
guide to start working with containers and iterators.

Hope this help.
 
F

food4uk

Thanks flagos:
I am reading this book but could not find anything related with
customer iterator.
Is my question is too simple to ask or too simple to answer?

Thanks anyway.
Sheldon
 
E

eriwik

I am very new to STL and generic programming. PLEASE help.
I have my own class looks like:

class pixelProperty {
public:
// get-functions here...
private:
Point mPosition;
int mCenterID;
Color mColor;

}Basically the pixelProperty represents a 2D image data. To make it
more convenience for pixle random access, I would like to use the
Vector container in STL but with an "enhanced" iterator which give me
more convenience such as using a member function in the iterator,
myiterator::neighbor(x,y), which give me a 2d-style random access.

My questions are:
1) Can I acutally make my customer iterator by composition or inherit
from the vector::iterator (how about deriving from or composing the
vector container class - I read some posts in the forum but not fully
understand.... ) . if it is possible, could any one can be kind enough
to show me an example?

Either inheritance of composition should work, but from the iterators
and not the vector. It can be quite a lot of work if you are using
composition or have many special requirements.
2) I would like to use a customer a random access iterator with the
STL algorithm (such as find()), take the pixelProperty as the data
type, how can I implement this STL-compatible iterator?

Just make sure that your iterator has the same interface and
functionality as STL ones. To find documentation about the STL
iterators google for "wg21c++", the first result is the standard group,
go to paper ans find a draft (working copy) and download. The part of
interest is section 24.1 "Iterator requirements".
 
F

food4uk

Thanks Eriwik:
I have checked the document. Quite understandable , but still feel a
bit difficult for me to implement one without an example.
Anyone else can give a hand?

Sheldon .

"(e-mail address removed) 写é“:
"
 
E

eriwik

Thanks Eriwik:
I have checked the document. Quite understandable , but still feel a
bit difficult for me to implement one without an example.
Anyone else can give a hand?

Next time, type your reply below what you are replying to.

As for your problem, what exactly do you want your iterator to do that
a normal iterator does not, if you are just adding functionality a
normal inheritance should do most of the work for you, if you need to
change some of the behaviour normal inheritance and just overriding one
or two methods might do. Ask yourself in which way your iterator is
different from an STL-iterator and what special considerations your
iterator might have, which of the STL-iterator methods/operators will
work the same and which won't.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top