Код: Выделить всё
class Entity
{
public:
...
// coordinate mondo: la mappa del mondo
int worldX = 0, worldY = 0;
}
Код: Выделить всё
std::list entityList;
std::vector npc;
Код: Выделить всё
class NPC_OldMan : public Entity
Код: Выделить всё
for (int i = 0; i < npc.size(); i++)
{
if (!npc.at(i).name.empty())
{
entityList.push_back(npc.at(i));
}
}
т.е. если npc1 имеет worldY = 10, а npc2 имеет worldY = 5, то в списке сущностей я хочу иметь npc2, а затем npc1.
Использование следующего включает в себя:
Код: Выделить всё
#include
#include
#include
#include
Код: Выделить всё
std::sort(entityList.begin(), entityList.end(), [](Entity e1, Entity e2)
{
// Custom comparison logic
return e1.worldY < e2.worldY; // this sorts in ascending order
});
Код: Выделить всё
Severity Code Description Project File Line Suppression State Details Error C2676 binary '-': 'const std::_List_unchecked_iterator' does not define this operator or a conversion to a type acceptable to the predefined operator with [ _Ty=Entity ] My2DGame_21 C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\algorithm 8165 Код: Выделить всё
std::sort(entityList.begin(), entityList.end(), [](const auto& a, const auto& b) { return a.worldY < b.worldY; });
Подробнее здесь: https://stackoverflow.com/questions/785 ... -a-c-class
Мобильная версия