Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. How to sort the elements of a std::vector of pointers to instances of derived classes

How to sort the elements of a std::vector of pointers to instances of derived classes

Scheduled Pinned Locked Moved The Lounge
c++tutorialgraphicsgame-devdata-structures
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11251399
    wrote on last edited by
    #1

    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
    #include

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

    // Overload
    
    R P U 3 Replies Last reply
    0
    • U User 11251399

      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
      #include

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

      // Overload
      
      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #2

      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?"

      1 Reply Last reply
      0
      • U User 11251399

        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
        #include

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

        // Overload
        
        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        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.

        Advanced TypeScript Programming Projects

        1 Reply Last reply
        0
        • U User 11251399

          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
          #include

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

          // Overload
          
          U Offline
          U Offline
          User 11251399
          wrote on last edited by
          #4

          OK

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups