Hi,
I am using #include <list>
Is there a command whereby I can reserve capacity for the list I am seeking to create?
Thanks
Hi,
I am using #include <list>
Is there a command whereby I can reserve capacity for the list I am seeking to create?
Thanks
Why do you want to do that? The reserve() member function of std::vector comes in handy to avoid invalidating iterators and to avoid reallocating when unnecessary. For std::list, deletion of elements only invalidates iterators to the deleted elements, and there is no linear time reallocation that happens from time to time.Originally Posted by paulogrady
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I want to add pointers to objects to a list to keep track of them.
Code:void GameEngine::AddSprite(Sprite* pSprite) { // Add a sprite to the sprite list if (pSprite != NULL) { // See if there are sprites already in the sprite list if (m_lpSprites.size() > 0) { // Find a spot in the sprite list to insert the sprite accoridng to its z-order // Sprites are placed in the dynamic list according ot their Z value so that they are drawn either on top or below other sprites bmp's list<Sprite*>::iterator siSpriteIterator; for (siSpriteIterator = m_lpSprites.begin(); siSpriteIterator != m_lpSprites.end(); siSpriteIterator++) if (pSprite->GetZPosition() < (*siSpriteIterator)->GetZPosition()) { // Insert the sprite into the sprite list m_lpSprites.insert(siSpriteIterator, pSprite); return; } } m_lpSprites.push_back(pSprite); } }
Why do you want to do that? The reserve() member function of std::vector comes in handy to avoid invalidating iterators and to avoid reallocating when unnecessary. For std::list, deletion of elements only invalidates iterators to the deleted elements, and there is no linear time reallocation that happens from time to time.Originally Posted by paulogrady
EDIT:
Incidentally, I think that you have a bug in your implementation of GameEngine::AddSprite. What happens if the Z value of the sprite to be inserted is greater than that of all the sprites whose pointers are already stored in the linked list? It looks like that new sprite will not have its pointer inserted.
I suggest using a standard generic algorithm with a comparator function object, e.g.,
Actually, this way I think that you no longer need to check if there are sprites already in the list, since the insertion at m_lpSprites.end() will be correct. Of course, note that I have not made any attempts to test my code example.Code:namespace { class CompareSpriteZPosition { public: CompareSpriteZPosition(Sprite* sprite) : sprite(sprite) {} bool operator()(Sprite* other) const { return sprite->GetZPosition() < other->GetZPosition(); } private: Sprite* sprite; }; } void GameEngine::AddSprite(Sprite* pSprite) { // Add a sprite to the sprite list if (pSprite != NULL) { // See if there are sprites already in the sprite list if (!m_lpSprites.empty()) { // Find a spot in the sprite list to insert the sprite accoridng to // its z-order. Sprites are placed in the dynamic list according to // their Z value so that they are drawn either on top or below other // sprites bmp's. m_lpSprites.insert( std::find_if(m_lpSprites.begin(), m_lpSprites.end(), CompareSpriteZPosition(pSprite)), pSprite); } else { m_lpSprites.push_back(pSprite); } } }
Last edited by laserlight; 03-15-2009 at 09:31 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
if possible
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Oh wait, I just realised that I accidentally edited my first reply to add in my code suggestion for my second reply
Anyway, my original second reply was also: just do it! ("Yes, and to do that you do not need to reserve capacity for the doubly linked list beforehand.")
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)