Pointers<\/a><\/p>\nOne of the disadvantages of storing an object inside of a vector is that whenever it gets moved, it has to copy the entire object to its new location. For small vectors, this may be possible, but when you start to get larger vectors, it becomes unacceptable. The way to avoid this is to use pointers. A pointer is quite a bit smaller than most objects, so moving them around takes a lot less time. However, because pointers use memory you have allocated, you must also remember to free that memory when you are done.<\/p>\n
Declaring a vector to hold pointers to objects is fairly simple; you just replace the MyObject portion with the appropriate conversion:<\/p>\n
std::vector<MyObject*> vec;<\/p>\n
To put something into the vector, all you really have to do, from the last code, is add the new operator:<\/p>\n
\/\/ Stick 10 random MyObject’s into the vector for(int j = 0; j < 10; j++)<\/p>\n
{<\/p>\n
vec. push_back( new MyObject(rand()%20+1, rand()%120 + 1));<\/p>\n
}<\/p>\n
Sorting the vector is a bit different, because the sort operator will use a binary predicate that you specify or the < operator by default. Because a pointer is just an integer, the < operator will sort by the memory address and not the contents of the MyObject.<\/p>\n
#include <algorithm><\/p>\n
bool GreaterAgePtr( MyObject* l, MyObject *r) {<\/p>\n
return (l->GetAge() > r->GetAge());<\/p>\n<\/p>\n
sort(vec. begin(), vec. end(), GreaterAgePtr); \/\/ Sorts by age sort(vec. begin(), vec. end()); \/\/ Sorts by age using < (memory address)<\/p>\n
Searching has the same problem as sorting because it uses the == operator, except in this case there is a way around it:<\/p>\n
#include <algorithm><\/p>\n
bool operator==(MyObject *l, MyObject r) {<\/p>\n
if(l->GetAge() != r. GetAge()) return false; if(l->GetHeight() != r. GetHeight()) return false; return true;<\/p>\n
}<\/p>\n
std::vector<MyObject>::iterator j;<\/p>\n
= std::find(vec. begin(), vec. end(), MyObject(10, 120)); f(j!= vec. end()) \/\/We found it<\/p>\n
Again, you are overloading the equality operator (==) just as you did earlier. However, this time, it can compare a pointer to a MyObject object and compare a MyObject object to itself. After you are done with your vector of pointers, you must free the memory that you allocated. This is a fairly simple process:<\/p>\n
#include<algorithm><\/p>\n
<\/p>\n
}<\/p>\n
};<\/p>\n
std::for_each(vec. begin(), vec. end(), DeletePtr<MyObject>());<\/p>\n
The class DeletePtr with the member function operator () is called a function object. All it does is delete MyObject pointers. If you wanted to, you could make it delete integer pointers by simply changing this line:<\/p>\n
for_each(vec. begin(), vec. end(), DeletePtr<MyObject>());<\/p>\n
to:<\/p>\n
for_each(vec. begin(), vec. end(), DeletePtr<int>());<\/p>\n
Simple and easy (and useful too).<\/p>\n
Conclusion<\/a><\/p>\nIf you are wondering about some of the applications of vectors in games, I have an idea. My idea is for a simple scene-graph. If you derive all of your objects from some base object, you could use a vector of pointers to the base object. This would allow you to easily do up\u00addates, collisions, and rendering. Because you would know that all objects before the current object had already moved and had been collision tested, you wouldn\u2019t have to test against them for your cur\u00adrent object. Also, you could reuse vector elements, such as when a creature dies, so you set its vector element to an empty object, and when you need a new object, just reuse the empty ones.<\/p>\n
Finally I would recommend that you do some more research into template programming, especially pertaining to the Standard Tem\u00adplate Library. It has many types of containers, including deques, lists,<\/p>\n
<\/p>\n