Jump to content
  • Advertisement
Sign in to follow this  
littlekid

Basic STL questions

This topic is 4001 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

Hi, is it possible to cross convert a reverse_iterator with a normal iterator? e.g. ForwardIter = MyMap.rbegin().base(); (Not sure if it is syntatically correct) If it is possible to cross convert, how will the ++ / -- operator work on the converted iterators? Another question I have is, if i have a number of objects to be rendered each frame where object can be added or remove, should I: Method1 std::vector<OBJECT> ObjectList; std::vector<int> FreeObjectListID; - Hence if i remove a object from the list, i store the free location ID at the FreeObjectListID. - And when I add a object, I check for any free location ID and overwrite it. If not existing free ID is avaliable, then i just use ObjectList.push_back(); - And to render in each frame, i just iterate through the vector ObjectList and render them. Method2 std::map<int, OBJECT> ObjectList; - I would add and remove objects normally using the std::map, insert, [] and erase functions - To render in each frame, I would iterate through the map ObjectList and render them. Which method is a better suggestion, or are there better ways to implemnt it? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Or method 3, use a std::list<>, which can insert and erase in constant time and is perfectly efficient for traversing from start to end.

std::list<> only falls down on random access, but you don't appear to need that for what you have stated above.

Share this post


Link to post
Share on other sites
But Objects can be remove and inserted from the list. If I am to use std::list, won't the indexing of the object change?

Or is there a way i can erase an OBJECT from the std::list without knowing where it is stored in the list?

Share this post


Link to post
Share on other sites
Quote:
Original post by littlekid
But Objects can be remove and inserted from the list. If I am to use std::list, won't the indexing of the object change?
Yes (likewise with vectors). Keep in mind though that list elements don't really have 'indices', per se, as lists are not random access (you can use, say, std::advance() to find the object corresponding to a given 'index', but if you really want indexed access it's generally better just to use a vector).
Quote:
Or is there a way i can erase an OBJECT from the std::list without knowing where it is stored in the list?
Depending on the nature of your objects, you might be able to use list::remove().

There are actually a number of ways you can go about removing expired objects. Here is one way to go about it:
    m_entities.erase(
std::remove_if(
m_entities.begin(),
m_entities.end(),
boost::bind(&Entity::Update, _1, elapsedMS)
),
m_entities.end()
);
Basically, this updates all of the entities, and then removes those that are 'dead'. (Each entity is responsible for returning a value of 'true' from its update function when it's time for it be removed from the simulation.)

Share this post


Link to post
Share on other sites
You shouldn't use std::remove_if() on std::lists. Instead you should use std::list::remove_if(); it's a lot more efficient.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
You shouldn't use std::remove_if() on std::lists. Instead you should use std::list::remove_if(); it's a lot more efficient.


Is there any reason why the standard library doesn't specialise the std::remove_if template for std::list<>::iterators?

Share this post


Link to post
Share on other sites
I'm afraid you'd have to ask someone on the standards committee that one. I can give you guesses, but that's about it.

Share this post


Link to post
Share on other sites
I was just wondering was there some odd technical reason. God knows there are enough of them in C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
You shouldn't use std::remove_if() on std::lists. Instead you should use std::list::remove_if(); it's a lot more efficient.
For what it's worth, in my posted example (copied from my current project), m_entities is of type std::vector<boost::shared_ptr<Entity> >.

Sorry, I should have clarified that in my post.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
I was just wondering was there some odd technical reason. God knows there are enough of them in C++.


Well, if I had to guess it would because it's because doing so would require a list iterator be able to delete itself from the container without having a reference to the iterator's container. This is certainly technically possible, but not necessarily something they wanted as part of the mandated public interface for the list iterator, since actual iterator implementations are not something that the standards committee wanted to dictate.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!