How to sort the elements of a std::vector of pointers to instances of derived classes
-
I'm programming a 2D RPG game that uses an array of derived class instance pointers. For clarity I provide a code example in C++. I would like to sort the elements of the entities array based on the worldY value of each element.
main.cpp:
/*
* result:
* player
* object
* object
* npc
* npc
* 2 npc y: 30
* 1 npc y: 6
* 1 object y: 20
* 2 object y: 10
* player y: 0
*/#include #include #include #include "Entity.h"
#include "Npc.h"
#include "Player.h"
#include "Object.h"int main()
{
Player* player = new Player();std::vector objs; std::vector npcs; std::vector entities; Entity\* tmp = new OBJ\_o(); objs.push\_back(tmp); objs.at(0)->worldY = 20; objs.at(0)->ID = "1"; tmp = new OBJ\_o(); objs.push\_back(tmp); objs.at(1)->worldY = 10; objs.at(1)->ID = "2"; tmp = new NPC(); npcs.push\_back(tmp); npcs.at(0)->worldY = 6; npcs.at(0)->ID = "1"; tmp = new NPC(); npcs.push\_back(tmp); npcs.at(1)->worldY = 30; npcs.at(1)->ID = "2"; //--------------- update ------------------- player->update(); for (auto iter\_objs = objs.begin(); iter\_objs != objs.end(); ++iter\_objs) { (\*iter\_objs)->update(); } for (auto iter\_npcs = npcs.begin(); iter\_npcs != npcs.end(); ++iter\_npcs) { (\*iter\_npcs)->update(); } //-------------- entities -------------------- entities.push\_back(player); for (int i = 0; i < objs.size(); i++) { if (!objs.at(i)->name.empty()) { entities.push\_back(objs.at(i)); } } for (int i = 0; i < npcs.size(); i++) { if (!npcs.at(i)->name.empty()) { entities.push\_back(npcs.at(i)); } } // sort entities std::sort(entities.begin(), entities.end()); //---------------- draw entities ------------------ for (auto iter = entities.begin(); iter != entities.end(); ++iter) { std::cout << (\*iter)->ID << " "; (\*iter)->draw(); } return 0;
}
Entity.h
#pragma once
#includeclass Entity
{
public:
int worldY = 0;
std::string name;
std::string ID;public:
virtual void update() { std::cout << "base" << std::endl; }
virtual void draw() { std::cout << "base y: -\n"; }// Overload
-
I'm programming a 2D RPG game that uses an array of derived class instance pointers. For clarity I provide a code example in C++. I would like to sort the elements of the entities array based on the worldY value of each element.
main.cpp:
/*
* result:
* player
* object
* object
* npc
* npc
* 2 npc y: 30
* 1 npc y: 6
* 1 object y: 20
* 2 object y: 10
* player y: 0
*/#include #include #include #include "Entity.h"
#include "Npc.h"
#include "Player.h"
#include "Object.h"int main()
{
Player* player = new Player();std::vector objs; std::vector npcs; std::vector entities; Entity\* tmp = new OBJ\_o(); objs.push\_back(tmp); objs.at(0)->worldY = 20; objs.at(0)->ID = "1"; tmp = new OBJ\_o(); objs.push\_back(tmp); objs.at(1)->worldY = 10; objs.at(1)->ID = "2"; tmp = new NPC(); npcs.push\_back(tmp); npcs.at(0)->worldY = 6; npcs.at(0)->ID = "1"; tmp = new NPC(); npcs.push\_back(tmp); npcs.at(1)->worldY = 30; npcs.at(1)->ID = "2"; //--------------- update ------------------- player->update(); for (auto iter\_objs = objs.begin(); iter\_objs != objs.end(); ++iter\_objs) { (\*iter\_objs)->update(); } for (auto iter\_npcs = npcs.begin(); iter\_npcs != npcs.end(); ++iter\_npcs) { (\*iter\_npcs)->update(); } //-------------- entities -------------------- entities.push\_back(player); for (int i = 0; i < objs.size(); i++) { if (!objs.at(i)->name.empty()) { entities.push\_back(objs.at(i)); } } for (int i = 0; i < npcs.size(); i++) { if (!npcs.at(i)->name.empty()) { entities.push\_back(npcs.at(i)); } } // sort entities std::sort(entities.begin(), entities.end()); //---------------- draw entities ------------------ for (auto iter = entities.begin(); iter != entities.end(); ++iter) { std::cout << (\*iter)->ID << " "; (\*iter)->draw(); } return 0;
}
Entity.h
#pragma once
#includeclass Entity
{
public:
int worldY = 0;
std::string name;
std::string ID;public:
virtual void update() { std::cout << "base" << std::endl; }
virtual void draw() { std::cout << "base y: -\n"; }// Overload
Please read the red text a little bit above your post. It talks about how this is not the appropriate place for a post of this nature.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
-
I'm programming a 2D RPG game that uses an array of derived class instance pointers. For clarity I provide a code example in C++. I would like to sort the elements of the entities array based on the worldY value of each element.
main.cpp:
/*
* result:
* player
* object
* object
* npc
* npc
* 2 npc y: 30
* 1 npc y: 6
* 1 object y: 20
* 2 object y: 10
* player y: 0
*/#include #include #include #include "Entity.h"
#include "Npc.h"
#include "Player.h"
#include "Object.h"int main()
{
Player* player = new Player();std::vector objs; std::vector npcs; std::vector entities; Entity\* tmp = new OBJ\_o(); objs.push\_back(tmp); objs.at(0)->worldY = 20; objs.at(0)->ID = "1"; tmp = new OBJ\_o(); objs.push\_back(tmp); objs.at(1)->worldY = 10; objs.at(1)->ID = "2"; tmp = new NPC(); npcs.push\_back(tmp); npcs.at(0)->worldY = 6; npcs.at(0)->ID = "1"; tmp = new NPC(); npcs.push\_back(tmp); npcs.at(1)->worldY = 30; npcs.at(1)->ID = "2"; //--------------- update ------------------- player->update(); for (auto iter\_objs = objs.begin(); iter\_objs != objs.end(); ++iter\_objs) { (\*iter\_objs)->update(); } for (auto iter\_npcs = npcs.begin(); iter\_npcs != npcs.end(); ++iter\_npcs) { (\*iter\_npcs)->update(); } //-------------- entities -------------------- entities.push\_back(player); for (int i = 0; i < objs.size(); i++) { if (!objs.at(i)->name.empty()) { entities.push\_back(objs.at(i)); } } for (int i = 0; i < npcs.size(); i++) { if (!npcs.at(i)->name.empty()) { entities.push\_back(npcs.at(i)); } } // sort entities std::sort(entities.begin(), entities.end()); //---------------- draw entities ------------------ for (auto iter = entities.begin(); iter != entities.end(); ++iter) { std::cout << (\*iter)->ID << " "; (\*iter)->draw(); } return 0;
}
Entity.h
#pragma once
#includeclass Entity
{
public:
int worldY = 0;
std::string name;
std::string ID;public:
virtual void update() { std::cout << "base" << std::endl; }
virtual void draw() { std::cout << "base y: -\n"; }// Overload
As many people are about to tell you. There are no programming questions in the lounge. You should probably post this in the Quick Answers instead.
-
I'm programming a 2D RPG game that uses an array of derived class instance pointers. For clarity I provide a code example in C++. I would like to sort the elements of the entities array based on the worldY value of each element.
main.cpp:
/*
* result:
* player
* object
* object
* npc
* npc
* 2 npc y: 30
* 1 npc y: 6
* 1 object y: 20
* 2 object y: 10
* player y: 0
*/#include #include #include #include "Entity.h"
#include "Npc.h"
#include "Player.h"
#include "Object.h"int main()
{
Player* player = new Player();std::vector objs; std::vector npcs; std::vector entities; Entity\* tmp = new OBJ\_o(); objs.push\_back(tmp); objs.at(0)->worldY = 20; objs.at(0)->ID = "1"; tmp = new OBJ\_o(); objs.push\_back(tmp); objs.at(1)->worldY = 10; objs.at(1)->ID = "2"; tmp = new NPC(); npcs.push\_back(tmp); npcs.at(0)->worldY = 6; npcs.at(0)->ID = "1"; tmp = new NPC(); npcs.push\_back(tmp); npcs.at(1)->worldY = 30; npcs.at(1)->ID = "2"; //--------------- update ------------------- player->update(); for (auto iter\_objs = objs.begin(); iter\_objs != objs.end(); ++iter\_objs) { (\*iter\_objs)->update(); } for (auto iter\_npcs = npcs.begin(); iter\_npcs != npcs.end(); ++iter\_npcs) { (\*iter\_npcs)->update(); } //-------------- entities -------------------- entities.push\_back(player); for (int i = 0; i < objs.size(); i++) { if (!objs.at(i)->name.empty()) { entities.push\_back(objs.at(i)); } } for (int i = 0; i < npcs.size(); i++) { if (!npcs.at(i)->name.empty()) { entities.push\_back(npcs.at(i)); } } // sort entities std::sort(entities.begin(), entities.end()); //---------------- draw entities ------------------ for (auto iter = entities.begin(); iter != entities.end(); ++iter) { std::cout << (\*iter)->ID << " "; (\*iter)->draw(); } return 0;
}
Entity.h
#pragma once
#includeclass Entity
{
public:
int worldY = 0;
std::string name;
std::string ID;public:
virtual void update() { std::cout << "base" << std::endl; }
virtual void draw() { std::cout << "base y: -\n"; }// Overload
OK