-
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.
-
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?
-
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
-
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.
-
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.
-
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
-
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){
// ....?
}
};
};