   Another Vector Question
  When you add an element to a vector dynamically as an "Object", say with a new operator...How do you determine where each instance of that object begins and ends?  I know you do somehting like thisfor ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )v1.erase( v1.begin( ) );But when you're dynamically alloacting objects to a vector, you don't know the new name of the dynamically allocated element, or do you?I understand that you use the erase() operator to actually physically erase the thing from the vector, but when you use a new operator and you aren't allocating the memory statically say with a SceneObjects.push_back(Object1), and instead are doing a SceneObjects.push_back(new Object).  How do you determine the beginning and end of each individual element, which almost always will differ in size?Let me add some of my code to show you what I mean...#include "World.h"std::vector<Object*> Scene_Objects;std::vector<Object*>::iterator Index;void World::AddObjectToWorld(){Scene_Objects.push_back(new Object);}The reason we dynamically allocate a new object each time is because we'll never know how many of what kind of any object we want in our game world.  You could just Statically allocate seperate objects, but then you'd have a whole bunch of unused instances of objects that you may or may not use..The problem with dynamically allocating a new object every time I need to is this....  As Scene_Objects grows I will ultimately have to delete some things that aren't needed on the scene anymore,  If I were to statically allocate objects I could just go Object1.erase() and make it easy, but since I don't know the index of each object I can't do just that, I have to figure out where each object is in the vector before I can delete them...How do I check what Scene_Objects[1] is?  Even better, if I say I need to delete a ship1 object from the scene, how do I quickly figure out where that ship1 object is in the vector and delete it?[Edited by - Shamino on December 14, 2005 11:54:42 AM]
  Your question has several parts and depending on the architecture of your engine the answer might differ.How do you determine what needs to be removed from the vector?Something I have done in the past has been to add a field to my base object:class CGameObject{// blablablabool IsMarkedForDeletion();void MarkForDeletion();}For dynamic allocation removal the loop would look like the following:for (iter = scene.begin(); iter!=scene.end(); ++iter){    if ((*iter)->MarkedForDeletion())    {   delete (*iter);   (*iter) = NULL;    }    iter = scene.erase(iter);}Even better, use a smart pointer that is compatible with STL containers (NOT std::auto_ptr) and then you can use erase without worrying about the delete.  boost::shared_ptr would work fine for this.Hope this helps   ;  I am not sure that a vector is necessarily the data structure that would suit best if you don't keep track of what index you place an object.If you know the index, that accessing the object is as simple as *(Scene_Objects[idx]) which would be your Object. Deleting would bedelete Scene_Objects[idx];after that you might be able to Scene_Objects.erase(idx); but I think most implementations require to pass an iterator but then you could do something like Scene_Objects.erase(Scene_Objects.begin() + idx);If the indeces aren't important to you, instead of using erase, you can swap that position and the back and call pop_back() which would save some time, but usually you keep track of the indeces somewhere else so you can access the vector faster.   ;  I like Ideej's idea!:D   ;  ldeej's code is incorrect. The return value from erase already is incremented, causing the loop's increment to skip the element that follows a deleted element.Also, since you're erasing, setting the pointer to NULL is unnecessary and wasteful -- it'll be overwritten anyway.for (iter = scene.begin(); iter!=scene.end();){    if ((*iter)->MarkedForDeletion())    {   delete *iter;   iter = scene.erase(iter);    }    else    {   ++iter;    }};  Or you can save yourself some headaches and use  boost::ptr_vector or a vector of smart pointers.   ;  well, Fruny...I could just add some error catching to fix thatif element 5 is marked for deletion, delete it, and then iterator-- to put me back in the right placeOh, I understand now Fruny, I'll use your code...;  So here is my World Class, the world class manages my scene!#include "World.h"std::vector<Object*> Scene_Objects;std::vector<Object*>::iterator Iter;void World::AddObjectToWorld(){Scene_Objects.push_back(new Object);}void World::RemoveObjectFromWorld(){for (Iter = Scene_Objects.begin(); Iter!=Scene_Objects.end();){    if ((*Iter)->MarkedForDeletion)    {   delete *Iter;   Iter = Scene_Objects.erase(Iter);    }    else    {   ++Iter;    }}};void World::RemoveAllObjectsFromWorld(){Scene_Objects.clear();};Here is the .h file#include "Object.h"#ifndef WORLD_H#define WORLD_Hextern std::vector<Object*> Scene_Objects;class World{public:void AddObjectToWorld();void RemoveAllObjectsFromWorld();void RemoveObjectFromWorld();};#endifIt seems like I have a good start to my Scene Management;Quote:Original post by Shaminovoid World::RemoveAllObjectsFromWorld(){Scene_Objects.clear();};It seems like I have a good start to my Scene ManagementIt seems you have a nice memory leak here [smile].You need to call delete on your raw pointers, otherwise the memory won't be released and destructors won't be called.void World::RemoveAllObjectsFromWorld(){   for ( size_t i = 0; i < Scene_Objects.size( ); ++i ) delete Scene_Objects;Scene_Objects.clear();};;Quote:Original post by darookieIt seems you have a nice memory leak here [smile].You need to call delete on your raw pointers, otherwise the memory won't be released and destructors won't be called.void World::RemoveAllObjectsFromWorld(){   for ( size_t i = 0; i < Scene_Objects.size( ); ++i ) delete Scene_Objects;Scene_Objects.clear();};Omg, I'm pwnzored, thanks for pointing that out (thats what shows I've only been working in C++ for a year)