STL customer iterator

Discussion in 'C++' started by cai_xiaodong@hotmail.com, Dec 5, 2006.

  1. Guest

    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
     
    , Dec 5, 2006
    #1
    1. Advertising

  2. flagos Guest

    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.
     
    flagos, Dec 5, 2006
    #2
    1. Advertising

  3. food4uk Guest

    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

    flagos wrote:
    > 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.
     
    food4uk, Dec 5, 2006
    #3
  4. Guest

    On Dec 5, 11:58 am, wrote:
    > 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".

    --
    Erik Wikström
     
    , Dec 5, 2006
    #4
  5. food4uk Guest

    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 .

    " 写é“:
    "
    > On Dec 5, 11:58 am, wrote:
    > > 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".
    >
    > --
    > Erik Wikström
     
    food4uk, Dec 5, 2006
    #5
  6. Guest

    On Dec 5, 2:55 pm, "food4uk" <> wrote:
    > 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.

    --
    Erik Wikström
     
    , Dec 5, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,429
  2. greg
    Replies:
    6
    Views:
    459
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    652
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,172
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,050
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page