Declaring functors for comparison?

I saw other people's questions, but did not find a single one that applies to what I am trying to achieve here.

I am trying to sort objects through my EntityManager class using std :: sort and std::vector<Entity *>

/*Entity.h*/
class Entity
{
public:
 float x,y;
};

struct compareByX{
 bool operator()(const GameEntity &a, const GameEntity &b)
 {
  return (a.x < b.x);
 }
};   

/*Class EntityManager that uses  Entitiy*/

typedef std::vector<Entity *> ENTITY_VECTOR; //Entity reference vector

class EntityManager: public Entity
{
private:
 ENTITY_VECTOR managedEntities;

public:
 void sortEntitiesX();
};

void EntityManager::sortEntitiesX()
{

 /*perform sorting of the entitiesList by their X value*/
 compareByX comparer;

 std::sort(entityList.begin(), entityList.end(), comparer);
}

I get a dozen errors like

: error: no match for call to '(compareByX) (GameEntity* const&, GameEntity* const&)'
: note: candidates are: bool compareByX::operator()(const GameEntity&, const GameEntity&)

I'm not sure, but ENTITY_VECTOR std::vector<Entity *>, and I don't know if this could be a problem when using the compareByX functor?

I am new to C ++, so welcome any help.

+3
source share
6 answers

... , , : const & GameEntity. vector<GameEntity*> const GameEntity*.

+3

- , (), "" , :

struct functor { 
   bool operator()(Entity const &a, Entity const &b) {
       return a.x < b.x;
   }
};

, Entity :

class Entity { 
    float x;
public:
    friend class byX;
    class byX {
        bool operator()(Entity const &a, Entity const &b) { 
            return a.x < b.x;
        }
    };
};

:

std::sort(ManagedEndities.begin(), ManagedEntities.end(), Entity::byX());

, X, < :

class Entity { 
     float x;
public:
     bool operator<(Entity const &other) { 
         return x < other.x;
     }
};

:

std::sort(ManagedEntities.begin(), ManagedEntities.end());

- Entity , , sort, - - std:: mem_fun_ref; , .

+3

, ....

- : , sort, - -. : . , Entity::compareByX, .

, , , mem_fun mem_fun_ref, "" -.

, Entity , sort ( ):

struct EntityComp {
  bool operator()( const GameEntity& a, const GameEntity& b ) const { 
    return a.x < b.x;
  }
}


...
std::sort( v.begin(), v.end(), EntityComp() );
+1

, compareByX static

+1

", ", ... , , GameEntity::x GameEntity::y .

- , , :

struct CompareX {
   bool operator()( const GameEntity& a, const GameEntity& b ) const {
      return a.x < b.x;
   }
};

struct CompareY {
   bool operator()( const GameEntity& a, const GameEntity& b ) const {
      return a.y < b.y;
   }
};

CompareX compx; // create a compare object
std::sort( v.begin(), v.end(), compx );

"" :

#include <iostream>

using namespace std;

// a mockup of your class
struct GameEntity { float x, y, z; };

// just to be able to print it...
ostream& operator<<( ostream& o, const GameEntity& g ) {
  return o << "(" << g.x << ", " << g.y << ", " << g.z << ")";
}

// cumbersome starts here...
typedef float (GameEntity::*membervar);

// a 'generic' float-member comparator
template< membervar m > struct CompareBy {
   bool operator()( const GameEntity& a, const GameEntity& b ) const {
      return a.*m < b.*m ;
   }
};

// example code
int main() {
   using namespace std;
   GameEntity v[] = { {1,0,0}, {2,0,1}, {3,-1,2} };
   GameEntity* vend = v + sizeof(v)/sizeof(v[0]);

   sort( v, vend, CompareBy< &GameEntity::x >() );
   copy( v, vend, ostream_iterator<GameEntity>( cout, "\n" ) );
}
+1

.

 class CompareByX
 {
   operator ()(const GameEntity &a, const GameEntity &b) { ... };
 };

 ...
 std::sort( this->begin(), this->end(), CompareByX);

, - - STL (), . ++, - STL.

Edit: Jerry's answer is better and more comprehensive.

0
source

Source: https://habr.com/ru/post/1723392/


All Articles