1

经过大量研究和尝试解决这个问题

我想知道是否有办法在 C++ 中订购对象列表;

到目前为止我看到的所有解决方案,我们必须事先定义哪个阵营将用于排序。

我想要的是让用户自由选择他想用哪个阵营来排序这是我的代码

class LOL
{
   public:
   LOL( const string& Nickname = "", int skill = 0,int level= 0 );
   bool operator > ( const LOL &rhs ) const; 
   void print() const;

   private:
   int level_;
   string Nickname_;
   int skill_;
};

inline
LOL::LOL( const string& Nickname, int skill, int level)
   : level_( level), Nickname_( Nickname ), skill_( skill )
{} 



inline
bool LOL::operator > ( const LOL& rhs ) const
{ return Nickname_  > rhs.Nickname_; } 



inline
void LOL::print() const
{ cout << Nickname_ << " from level " << level_
   << " has skill of [ " << skill_ <<" ]"<< endl<< endl;
}

int main( )
{
   list<LOL> list1;
   list1.push_back( LOL( "Dhespair", 50000, 30 ) );
   list1.push_back( LOL( "Pedro", 1, 1 ) );
   list1.push_back( LOL( "Blackblood", 99999, 30 ) );
   list1.push_back( LOL( "Zladovic", 30000, 25 ) );

   list1.sort( greater <LOL>()  );
   for_each( list1.begin(), list1.end(), mem_fun_ref( &LOL::print ) );
   printf("\n\n");
   system("pause");
}

经过几次尝试,我想出了这个解决方案

class LOL
{
   public:
   LOL( const string& Nickname = "", int skill = 0,int level= 0 , int op=1);
   bool operator > ( const LOL &rhs        ) const; // MUDAR O > para maior  ou < MENOR
   bool operator < ( const LOL &rhs        ) const; // MUDAR O > para maior  ou < MENOR
   void print() const;

   private:
   int level_;
   string Nickname_;
   int skill_;
   int op_;
};

inline
LOL::LOL( const string& Nickname, int skill, int level, int op)
   : level_( level), Nickname_( Nickname ), skill_( skill ), op_( op )
{} 



inline
bool LOL::operator > ( const LOL& rhs ) const
{ 
    switch(rhs.op_)
    {
    case 1:
    return Nickname_  > rhs.Nickname_; 
    break;

    case 2:
    return level_  > rhs.level_; 
    break;

    case 3:
    return skill_ > rhs.skill_; 
    break;
    } // MUDAR O  > para maior  ou < MENOR e ESCOLHER CAMPO A TER EM CONTA
}

inline
bool LOL::operator < ( const LOL& rhs ) const
{ 
    switch(rhs.op_)
    {
    case 1:
    return Nickname_  < rhs.Nickname_; 
    break;

    case 2:
    return level_  < rhs.level_; 
    break;

    case 3:
    return skill_ < rhs.skill_; 
    break;
    } // MUDAR O  > para maior  ou < MENOR e ESCOLHER CAMPO A TER EM CONTA
}


inline
void LOL::print() const
{ cout << Nickname_ << " from level " << level_
   << " has skill of [ " << skill_ <<" ]"<< endl<< endl;
}

int main( )
{
   list<LOL> list1;
   list1.push_back( LOL( "Dhespair", 50000, 30,3 ) );
   list1.push_back( LOL( "Pedro", 1, 1,3 ) );
   list1.push_back( LOL( "Blackblood", 99999, 30,3) );
   list1.push_back( LOL( "Zladovic", 30000, 25,3 ) );

   list1.sort( less <LOL>()  );
   for_each( list1.begin(), list1.end(), mem_fun_ref( &LOL::print ) );
   printf("\n\n");
   system("pause");
}

我所做的是对我的对象有一个新值,我将使用它来操作 bool 运算符内的 switch case。

它有效,但如果有人知道一种使用该开关的方法,而不必在我的对象中有一个新参数,我真的很感激 x)

此致

4

1 回答 1

0
bool compareTwoLOLObjectsWay1(LOL &A, LOL &B)
{
  /* Logic to determine if A<B */
}

bool compareTwoLOLObjectsWay2(LOL &A, LOL &B)
{
  /* Logic to determine if A>B */
}

switch(sortWay)
{
  case 1:
    list1.sort(&compareTwoLOLObjectsWay1);
    break;
  case 2:
    list1.sort(&compareTwoLOLObjectsWay2);
    break;
}
于 2013-01-10T22:37:19.330 回答