Writing Iterators...

This is a discussion on Writing Iterators... within the C++ Programming forums, part of the General Programming Boards category; Ok, i got a class called BOB. Im interested in writing iterators for bob so i could perform iterator operations ...

  1. #1
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265

    Writing Iterators...

    Ok, i got a class called BOB. Im interested in writing iterators for bob so i could perform iterator operations with mr BOB. Im not even sure if i need iterators to do this, but say i want to transverse a vector of BOBs, or sort that vector of bobs. Could i do this without iterators? What member functions do i need for BOB to be happy working with STL components? Thx.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    The way I understand it is an iterator is a smart pointer, and a smart pointer is an object that can be used to point to other objects that are organized in a complicated way. It hides the complication of accessing the object you are interested in, and when de-referenced it can simply return the object, or access its members through the -> operator, just like an ordinary pointer.

    So, it seems to me, if you're going to be using vectors, you don't need to write your own iterators. The vector class has them already.

    What member functions do i need for BOB to be happy working with STL components?

    I don't think you have to worry about that for any STL container. They all have their own iterators. After all, isn't that the whole point of using an STL container?

  3. #3
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265

    well...

    For example, to use the stl sort function it was my understanding that the objects being sorted must contain a less-than operator. Things like that are what i would be looking for a list of. What other operators and member functions would poor lonely BOB need to play with the others. =P

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The main thing you need to make Bob's play nice with the STL is a good copy ctor. Generally, if you have to write a copy ctor, you need to write the assignment and destructor as well. Assignment is also frequently a requirement, however you can also frequently avoid actually using any assignments as most containers will strongly perfer copy construction over assignment.

    A default ctor is also very usefull if your container supports resize, although you can work around this.

    You don't need to have a < for either sorting or sorted containers, if you don't have one you will need to write a binary predicate that defines a strict weak ordering

    If you cannot create a copy ctor/assignment, or if these are too expensive a common workaround is a container of pointers to Bob's. This is one of the situations where having a comparison functor comes into it's own.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Iterators are a generalized type of pointer, you won't need write any for your class.
    C++ intristicly provides a default constructor, destructor, copy constructor and assignment operator for classes you write, unless you specifically provide them yourself.
    You'll pretty much need to overload all operators if you want bob to be fully compatable with all STL algorithms and containers. But this is generally not needed.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    #include <vector>
    
    using namespace std;
    
    class Bob
    {
    public:
        void speak();
    // ...
    };
    
    int main()
    {
        vector<Bob> vec(20);
        vector<Bob>::iterator it; // The iterator.
    
        for(it = vec.begin(); it != vec.end(); ++it)
        {
            (*it).speak(); // Equivalently it->speak();
            // ....
        }
    
        return 0;
    }
    Perhaps I misunderstood the question, but it seems like that is what your looking for...

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    clue
    Guest
    template <class T>
    struct iterator {
    T base, * object;
    iterator(){object = &base; }
    iterator& operator = (T * p) {
    attach(p);
    return *this;
    }
    T * attach(T * p){
    if(p)object = p;
    else object = &base;
    return object;
    }
    T * operator(){ return object; }
    T * operator ++ (int){
    // ....?
    }
    };
































    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  2. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 02:16 PM
  3. Writing my own stl container
    By curos in forum C++ Programming
    Replies: 10
    Last Post: 12-18-2005, 03:33 AM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21