vectors vectors vectors list or map?

Started by
3 comments, last by aleisterbukowski 15 years, 11 months ago
I'm having a hard time trying to truly grasp vectors, list, and maps I have plenty of books, and online links for referencing, and I know what they do. I'm just having a hard time implementing it in my game. I have an Entity class that has a vector and a vector iterator

 //Our Entity List 
	 static vector<Entity*>     EntityList;
	 static vector<Entity*>::iterator EntityList_Interator;


Then I have as an object

//Global vars for our game!
static const int num_rows = 6;
static const int num_cols = 5;
static const int num_blkz = num_rows * num_cols;

//awards scene
static const int num_awardz = 5;
static const string awardz_directory[5] = {"images/5000_blur.png","images/10000_blur.png","images/blue_level_blur.png",
							  "images/second_waive_blur.png","images/complete_blur.png"};
static const int awdz_blk_size = 25;

//Entities
Entity Blockz[num_blkz];
Entity Awardz[5];


I have it created this way because when I change it to map or list or whatever I choose to decide to change it to I'll be able to create multiple objects of different multiple objects and add them to a single sprite_list that will let me know what is on, off of the screen what to push and pop in the list to render, etc... SO for the most part I think I'm designing it correctly. However I don't know how to delete the i'th object in the list using the iterator or without using it in the Blockz.EntityList Can someone please help?
Advertisement
For an std::list, if you have an iterator to the object you want to remove, you can use erase(). If you only have the object itself, you can use remove(). One other function worth mentioning is remove_if().
I was using erase(); but I didn't know how to use the iterator with it.

Like Do I need to call the iterator through the Blockz or by Entity::EntityList_Iterator by itself?

I was doing all sorts of things a noob would be doing when they don't know how to properly initialize and utilize what they're trying to use and like always I failed.
Here's an example usage:

class Obj {public:    Obj(int num):num(num) {}    int getNum() { return num; }private:    int num;};void func() {    list<Obj*> myList;    myList.push_back(new Obj(0));    myList.push_back(new Obj(1));    myList.push_back(new Obj(2));    // Remove all Objs with num == 1    list<Obj*>::it = myList.begin();    while (it != myList.end()) {        if (it->getNum() == 1)            // erase() returns the iterator to the element after the one            // it removes, so we don't increment it in this case            it = myList.erase(it);        else            ++it;    }}


This is just to show how to use erase(). For the above, you would usually do something like this:

bool pred(const Obj *obj) {    return obj->getNum() == 1;}myList.remove_if(pred);
Thanks ;) It really helps out looking at code using someone else's perspective and how they would code it.

This topic is closed to new replies.

Advertisement