STL basically says: valid implementation may be stateful or stateless.
C++11 added stateful allocators as a requirement. Question is... how many years until it becomes widely available?
Posted 15 May 2012 - 03:41 AM
List<Zombie> zombies;
uint zcount = zombies.Count();
for (uint i = 0; i < zcount; i++)
{
uint bcount = bullets.Count();
for (uint j = 0; j < bcount; j++)
{
if (collide(bullets[j], zombies[i]))
{
bullets.Remove(j);
zombies.Remove(i);
}
}
}
Posted 15 May 2012 - 03:50 AM
I don't use STL because I never figured out how to delete from a list while iterating through, so I made my own classes for List<>, Array<>, and Map<>
it = collection.erase(it);
Edited by Olof Hedman, 15 May 2012 - 03:55 AM.
Posted 15 May 2012 - 04:34 AM
Looking at your code, you still haven't. That loop is fundamentally wrong in a number of ways. In addition to the cases Olaf highlighted, what if multiple bullets collide with a given zombie in a single frame? You'll end up removing zombies who might be nowhere near the bullet.
I don't use STL because I never figured out how to delete from a list while iterating through
bool handleBulletCollision(vector<Bullet> &bullets, const Zombie &zombie)
{
for (auto i = bullets.begin() ; i != bullets.end() ; ++i)
{
if(collides(*i, zombie))
{
bullets.erase(i);
return true;
}
}
return false;
}
void handleZombieBulletCollisions(vector<Zombie> &zombies, vector<Bullet> &bullets)
{
auto i = zombies.begin();
while(i != zombies.end())
{
if(handleBulletCollision(bullets, *i))
{
i = zombies.erase(i);
}
else
{
++i;
}
}
}
void handleZombieBulletCollisions(vector<Zombie> &zombies, vector<Bullet> &bullets)
{
auto end = zombies.end();
auto i = remove_if(zombies.begin(), end, [&bullets](const Zombie &zombie) {
return handleBulletCollision(bullets, zombie);
});
zombies.erase(i, end);
}
That implementation involves less copies being made if multiple zombies are removed in a single pass.
Posted 15 May 2012 - 04:59 AM
for (uint i = 0; i < list.GetCount(); i++)
{
printf("List[%i] = %i\n", i, list.Get(i));
}
for (uint i = 0; i < list.GetCount(); i++)
{
if (list.Get(i) % 2 == 0)
{
list.Remove(i);
i--;
}
}
for (uint i = 0; i < list.GetCount(); i++)
{
printf("List[%i] = %i\n", i, list.Get(i));
}
Posted 15 May 2012 - 05:13 AM
#include <vector>
#include <cstdio>
#include <cstdlib>
int main()
{
std::vector<int> numbers;
for(unsigned i = 0 ; i < 20 ; ++i)
{
numbers.push_back(std::rand() % 50);
}
for (unsigned i = 0; i < numbers.size(); i++)
{
std::printf("List[%i] = %i\n", i, numbers[i]);
}
for (unsigned i = 0; i < numbers.size(); i++)
{
if (numbers[i] % 2 == 0)
{
numbers.erase(numbers.begin() + i);
i--;
}
}
for (unsigned i = 0; i < numbers.size(); i++)
{
std::printf("List[%i] = %i\n", i, numbers[i]);
}
}
Posted 15 May 2012 - 09:49 AM
Please quote where I said that.
Your original posts said one should not use lists at all, not that vectors are usually better.
If you're worried about performance, you should strongly consider not using linked lists at all.
... your linked lists, trees, etc., rather than just using LinkedList<MyClass> (or whatever the STL syntax is).
Edited by mdwh, 15 May 2012 - 09:52 AM.
Posted 15 May 2012 - 10:10 AM
Posted 15 May 2012 - 04:59 PM
Edited by freakchild, 15 May 2012 - 05:57 PM.
Posted 13 June 2012 - 07:47 PM
Posted 14 June 2012 - 01:45 AM
Quoted for emphasis; that is an excellent guideline.The biggest rule of thumb is this: If you have to ask, you should use STL.
- Jason Astle-Adams.
From my blog: 20 ways to advertise your game | What next? Intermediate to advanced C++
How to make games WITHOUT programming | 4 reasons you aren't a successful indie developer
Posted 15 June 2012 - 02:23 AM
- Jason Astle-Adams.
From my blog: 20 ways to advertise your game | What next? Intermediate to advanced C++
How to make games WITHOUT programming | 4 reasons you aren't a successful indie developer