Click here to Skip to main content
16,004,647 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Learning Assembly: The OS challenge Pin
honey the codewitch5-Jun-24 16:32
mvahoney the codewitch5-Jun-24 16:32 
GeneralRe: Learning Assembly: The OS challenge Pin
raddevus6-Jun-24 1:56
mvaraddevus6-Jun-24 1:56 
GeneralRe: Learning Assembly: The OS challenge Pin
jochance6-Jun-24 6:08
jochance6-Jun-24 6:08 
GeneralRe: Learning Assembly: The OS challenge Pin
Kornfeld Eliyahu Peter5-Jun-24 17:48
professionalKornfeld Eliyahu Peter5-Jun-24 17:48 
GeneralRe: Learning Assembly: The OS challenge Pin
Richard MacCutchan5-Jun-24 23:10
mveRichard MacCutchan5-Jun-24 23:10 
GeneralRe: Learning Assembly: The OS challenge Pin
theoldfool6-Jun-24 1:43
professionaltheoldfool6-Jun-24 1:43 
GeneralRe: Learning Assembly: The OS challenge Pin
abmv6-Jun-24 7:47
professionalabmv6-Jun-24 7:47 
QuestionHow to sort the elements of a std::vector of pointers to instances of derived classes Pin
Member 112856685-Jun-24 7:35
Member 112856685-Jun-24 7:35 
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:
<pre lang="C++">
/*
 * 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 <iostream>
#include <vector>
#include <algorithm>
#include "Entity.h"
#include "Npc.h"
#include "Player.h"
#include "Object.h"

int main()
{
    Player* player = new Player();

    std::vector<Entity*> objs;
    std::vector<Entity*> npcs;
    std::vector<Entity*> 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
C++
#pragma once
#include <iostream>

class 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"; }

    // Overloading < operator for std::sort in main: draw
    bool operator<(const Entity& obj) const
    {
        return worldY < obj.worldY;
    }
};

Npc.h
C++
#pragma once
#include "Entity.h"

class NPC : public Entity
{
public:
    NPC();
    void update() override;
    void draw() override;
};

Npc.cpp
C++
#include "Npc.h"
#include <iostream>

NPC::NPC() { name = "npc"; }
void NPC::update() { std::cout << name << std::endl; }
void NPC::draw() { std::cout << "npc y: " << worldY << std::endl; }

Player.h
C++
#pragma once
#include "Entity.h"

class Player : public Entity
{
public:
    Player();
    void update() override;
    void draw() override;
};

Player.cpp
C++
#include "Player.h"
#include <iostream>

Player::Player() { name = "player"; }
void Player::update() { std::cout << name << std::endl; }
void Player::draw() { std::cout << "player y: " << worldY << std::endl; }

Object.h
C++
#pragma once

#include "Entity.h"

class OBJ_o : public Entity
{
public:
    OBJ_o();
    void update() override;
    void draw() override;
};

Object.cpp
C++
#include "Object.h"
#include <iostream>

OBJ_o::OBJ_o() { name = "object"; }
void OBJ_o::update() { std::cout << name << std::endl; }
void OBJ_o::draw() { std::cout << "object y: " << worldY << std::endl; }

(see result put as comment at the beginning of Main.cpp).
How should I do an increasing order of y (worldY) ?

Using:
std::sort(entities.begin(), entities.end());

I don't get the desired result:

player
object
object
npc
npc
player y: 0
1 npc y: 6
2 object y: 10
1 object y: 20
2 npc y: 30

AnswerRe: How to sort the elements of a std::vector of pointers to instances of derived classes Pin
Rick York5-Jun-24 7:42
mveRick York5-Jun-24 7:42 
AnswerRe: How to sort the elements of a std::vector of pointers to instances of derived classes Pin
Pete O'Hanlon5-Jun-24 7:45
mvePete O'Hanlon5-Jun-24 7:45 
AnswerRe: How to sort the elements of a std::vector of pointers to instances of derived classes Pin
Member 112856685-Jun-24 7:59
Member 112856685-Jun-24 7:59 
GeneralOh Microsoft, you dullards.... Pin
charlieg5-Jun-24 2:53
charlieg5-Jun-24 2:53 
GeneralRe: Oh Microsoft, you dullards.... Pin
Richard Andrew x645-Jun-24 3:02
professionalRichard Andrew x645-Jun-24 3:02 
GeneralRe: Oh Microsoft, you dullards.... Pin
charlieg5-Jun-24 3:18
charlieg5-Jun-24 3:18 
GeneralRe: Oh Microsoft, you dullards.... Pin
dandy725-Jun-24 4:56
dandy725-Jun-24 4:56 
GeneralRe: Oh Microsoft, you dullards.... Pin
OriginalGriff5-Jun-24 5:49
mveOriginalGriff5-Jun-24 5:49 
GeneralRe: Oh Microsoft, you dullards.... Pin
charlieg5-Jun-24 6:49
charlieg5-Jun-24 6:49 
GeneralRe: Oh Microsoft, you dullards.... Pin
dandy725-Jun-24 7:56
dandy725-Jun-24 7:56 
GeneralRe: Oh Microsoft, you dullards.... Pin
lmoelleb5-Jun-24 9:10
lmoelleb5-Jun-24 9:10 
GeneralRe: Oh Microsoft, you dullards.... Pin
charlieg6-Jun-24 2:49
charlieg6-Jun-24 2:49 
GeneralI find it really satisfying to contribute to open source projects Pin
honey the codewitch5-Jun-24 0:44
mvahoney the codewitch5-Jun-24 0:44 
GeneralRe: I find it really satisfying to contribute to open source projects Pin
rob tillaart5-Jun-24 21:38
rob tillaart5-Jun-24 21:38 
GeneralRe: I find it really satisfying to contribute to open source projects Pin
honey the codewitch5-Jun-24 21:56
mvahoney the codewitch5-Jun-24 21:56 
GeneralRe: I find it really satisfying to contribute to open source projects Pin
Mark Starr6-Jun-24 2:28
professionalMark Starr6-Jun-24 2:28 
GeneralRe: I find it really satisfying to contribute to open source projects Pin
Shawn Eary May20216-Jun-24 3:11
Shawn Eary May20216-Jun-24 3:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.