Jump to content
  • Advertisement
Sign in to follow this  
AcidZombie24

stl vector like iterator 2

This topic is 3774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
AcidZombie, there's a ridiculously long-running bug on the forums which causes certain bits and pieces of code to do this to posts. I suggest that you post the code on this paste site and then link it here. I then suggest that you send about five emails to webmaster@gamedev.net asking them to fix the damn bug already.

Share this post


Link to post
Share on other sites
ok thanks
-----------
so i want to make a stl vector like iterator. i came up with this code.

1) if i want to do *b(25); how would i implement this?
2) i am sure begin() is wrong, it seems like what i do below is poor practice. How does stl (vector) really handle that? and yes, i look at the source :x. This is the closes i got

http://rafb.net/p/hPfxS051.html
-----------

since the site had an update (not to long after my post >_<), lets see this long post works below

Share this post


Link to post
Share on other sites
I just looked at your code and honestly, I have no idea what you're trying to do.

An iterator is supposed to behave like a pointer to an element in a container (like std::vector). It can "point" to a certain element in the container, can be "dereferenced" to get that element, and can be "moved" to traverse the container (depending on the container type, you may only be able to advance it one element at a time, or skip over several elements at once). Therefore, it's implementation is tightly coupled with the implementation of the container it's meant to work with (even though it's interface can be very generic, and in fact, that's the point of having an iterator in the first place).

In your code, there is no container, so what is your iterator supposed to do?

Share this post


Link to post
Share on other sites
More descriptive names are needed! It's very hard to work out what your code is for.

But some general advice.

operator++() (no int argument) should return a reference to *this.
operator++(int) should return a full object (not a reference) and can universally(?) be implemented as:

iterator operator++(int)
{
iterator ret(*this);
++*this;
return ret;
}


Why do your comparison operators return ints? What's wrong with bool?

Look up std::iterator<> and std::iterator_traits<>. Use one or the other so that your class interacts well with the standard library and other algorithms designed to use the same conventions.

The standard defines 5 different kinds of iterator:

* output
* input
* forward
* bidirectional
* random access

Decide which one yours is and implement the necessary interface. The further down the list you go, the more work it is to implement the required functionality for the given category.

But it's also true (except for input/output) that the further down the list you go, the greater the number of available algorithms can be used in conjunction with your iterators.

However! You may not want to implement one of the richer interfaces so that you allow room for maneuver in the implementation.

Share this post


Link to post
Share on other sites
Quote:
Original post by the_edd
iterator operator++(int)
{
iterator ret(*this);
++*this;
return ret;
}


Why do your comparison operators return ints? What's wrong with bool?


It an example, i wont be using int as input and i wont be returning int or bool.

Quote:
Original post by the_edd
The standard defines 5 different kinds of iterator:

* output
* input
* forward
* bidirectional
* random access


its either input or forward, i dont really know the difference between the two. It looks like mutablility is the difference so i'll say it is input. (now that i thinking about it, forward allows both in and output?)

Where can i learn more about Iterator standards or about Input Iterator? i look at the sgi stl site and it ... isnt a great resource.

when receiving the value via *classVar; i want an optional parameter. either to modify the return value (of *classVar) or to get extended information. I would be doing *classVar( /*something*/ ); but when i try creating that syntax, i get a illegal indirection or other errors. Maybe i am not allowed to do that?

I am about to do *classVar alone and classVar(/*something*/) but not at the same time. Is doing them both possible?

[Edited by - AcidZombie24 on February 19, 2008 10:27:29 AM]

Share this post


Link to post
Share on other sites
It might be worth having a look at Boost's iterator helper stuff. It allowed me to knock up some memory iterators pretty quickly for my texture loading library.

Share this post


Link to post
Share on other sites
The free book in the third link in my sig (volume 2) has lots of info on the STL, including iterators.

Share this post


Link to post
Share on other sites
Quote:
Original post by AcidZombie24
Quote:
Original post by the_edd
iterator operator++(int)
{
iterator ret(*this);
++*this;
return ret;
}


Why do your comparison operators return ints? What's wrong with bool?


It an example, i wont be using int as input and i wont be returning int or bool.


Huh? I mean your operator!= returns an int. It should return a bool. I'm not sure what you mean by "won't be using int as input"?

Quote:

its either input or forward, i dont really know the difference between the two. It looks like mutablility is the difference so i'll say it is input. (now that i thinking about it, forward allows both in and output?)


Yes, mutability in the sense that you can't modify what an input iterator refers to. You can only pull stuff out of it. A forward iterator implements the union of input and output iterators (more or less), as you suggest.

Quote:

Where can i learn more about Iterator standards or about Input Iterator? i look at the sgi stl site and it ... isnt a great resource.


Nicolai Josuttis' The C++ Standard Library has some easily digestible information. I haven't had the need to search for this info online, so I'm afraid I can't suggest anything. You could try dinkumware, maybe?

Quote:

when receiving the value via *classVar; i want an optional parameter. either to modify the return value (of *classVar) or to get extended information. I would be doing *classVar( /*something*/ ); but when i try creating that syntax, i get a illegal indirection or other errors. Maybe i am not allowed to do that?


Yeah all overloaded operators, except operator(), allow only a small range of signatures. You've wandered outside that range, here.

If you want whatever operator* gives you to have more information, then return a class with that information in it.

As phantom suggested, the boost iterator adaptors and framework is quite useful (once you stare at it long enough :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!