I’m writing a C++ program for a 2D RPG game, using the SDL2 libraries. I have a class named Entity that has a property named worldY:
class Entity
{
public:
...
// coordinate mondo: la mappa del mondo
int worldX = 0, worldY = 0;
}
In another class GamePanel I declare one of its properties:
std::list<Entity> entityList;
std::vector<Entity> npc;
The NPC vector is filled with instances of the:
class NPC_OldMan : public Entity
in this way:
for (int i = 0; i < npc.size(); i++)
{
if (!npc.at(i).name.empty())
{
entityList.push_back(npc.at(i));
}
}
Now I would like the entityList to be sorted in ascending order by the worldY property.
i.e. if npc1 has worldY = 10 and npc2 has worldY = 5, then in the entityList I want to have npc2 and then npc1.
Using the following includes:
#include <algorithm>
#include <iostream>
#include <list>
#include <iterator>
I was thinking of using this:
std::sort(entityList.begin(), entityList.end(), [](Entity e1, Entity e2)
{
// Custom comparison logic
return e1.worldY < e2.worldY; // this sorts in ascending order
});
But I can’t compile it, because it gives me this error:
Severity Code Description Project File Line Suppression State Details Error C2676 binary '-': 'const std::_List_unchecked_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' does not define this operator or a conversion to a type acceptable to the predefined operator with [ _Ty=Entity ] My2DGame_21 C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includealgorithm 8165
I also tried to do this, but the result doesn’t change, I always get the same error:
std::sort(entityList.begin(), entityList.end(), [](const auto& a, const auto& b) { return a.worldY < b.worldY; });
How should I fix my code? I do not know how to do. Is there another way to do this sorting of the list ?
2
Answers
You can’t do that with
std::list
.std::sort
requires a pair of random access iterator andstd::list
is obviously not the case.Most probably you need to use
std::vector
instead.You can sort
std::list
using its member functionsort()
:However, if you don’t have a special reason to use
std::list
, usestd::vector
instead. In this case, the source code you showed will work.