使用单个重载运算符对STL列表进行排序< [英] Sort STL list with single overloaded operator <

查看:97
本文介绍了使用单个重载运算符对STL列表进行排序<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功使用文本文件中的数据填充链接列表。链接列表包含的结构有5个字段,类型为 string



我希望按一定的顺序对列表进行排序结构字段(升序或降序)。我决定重载运算符<但是我不知道如何实现它。



到目前为止,我能够按一个固定字段对列表进行排序。以下是相关代码:

I have successfully populated linked list with data from a text file. Linked list contains structure that has 5 fields of type string.

I wish to sort the list by a certain structure field ( ascending or descending ). I have decided to overload operator < but I do not know how to implement it.

So far I am able to sort list by one fixed field. Here is the relevant code:

#include <iostream>       
#include <list>           // std::list
#include <fstream>        // std::ifstream, std::ofstream
#include <string>         // std::string
#include <algorithm>      // std::remove, std::remove_if
#include <sstream>        // std::istringstream

class Lista
{
private:
    struct Person
    {
        // data from file
        std::string lastName;  // other fields omitted for keeping brevity

        // operator < ( needed for correctly sorting the linked list )
        bool operator< ( const Person &p )const
        {
            return lastName > p.lastName;
        }

        // constructor / destructor ... omitted for keeping brevity 
    };

    // linked list that holds all the data from files
    std::list<Person> persons;

public:

// constructor / destructor ... omitted for keeping brevity

// method for sorting the list
    void sortList()
    {
        persons.sort();
    }
};





我想添加 enum 的选择,所以我只能使用一个重载运算符<用于分类。



这样的东西:





I would like to add enum of choices, so I can use only one overloaded operator < for sorting.

Something like this:

class Lista
{
private:
    struct Person
    {
        //================ here I could add enum of choices ============//

        enum choice { FIELD1, LASTNAME, FIELD2 }; // you get the point
        bool ascending;  // decides if it is ascending or descending

        //==============================================================//

        // data from file
        std::string lastName;  // other fields omitted for keeping brevity

        // operator < ( needed for correctly sorting the linked list )
        bool operator< ( const Person &p )const
        {
            if ( choice == FIELD1 )  
                return field1 < p.field1;

            if ( choice == FIELD2 )  
                return field2 < p.field2;

            if ( choice == LASTNAME )  
                 return lastName > p.lastName;
        }

        // constructor / destructor ... omitted for keeping brevity 
    };

    // linked list that holds all the data from files
    std::list<Person> persons;

public:

// constructor / destructor ... omitted for keeping brevity

// method for sorting the list
    void sortList( Person::choice ch, bool ascending)
    {
        // here I should use the parameters to invoke proper sorting 
        persons.sort();
    }





我真的不知道该怎么做,能帮帮我吗?



I really have no idea how to do this, can you please help me?

推荐答案

我不会选择里面的排序函数。我会使用不同的比较器。例如,看看下面的代码:

I wouldn't choose the different path inside the sort function. I would use instead different comparators. For instance, have a look at the following code:
#include <list>
#include <string>
#include <iostream>
using namespace std;

struct my_struct
{
  my_struct(int i, string s):i(i),s(s){}
  int i;
  string s;
};

struct CBIA
{
  bool operator()(const my_struct & m1, const my_struct & m2){ return m1.i<m2.i; }
};

struct CBID
{
  bool operator()(const my_struct & m1, const my_struct & m2){ return m1.i>m2.i; }
};

struct CBSA
{
  bool operator()(const my_struct & m1, const my_struct & m2){ return m1.s<m2.s; }
};
struct CBSD
{
  bool operator()(const my_struct & m1, const my_struct & m2){ return m1.s>m2.s; }
};


int main()
{
  list < my_struct> l;

  l.push_back( my_struct(5, string("alpha")));
  l.push_back( my_struct(1, string("gamma")));
  l.push_back( my_struct(20, string("delta")));
  l.push_back( my_struct(-100, string("beta")));

  for (;;)
  {
    int i;
    cout << "sort: " << endl << " 0 by int asc" << endl << " 1 by int desc" << endl << " 2 by string asc" << endl << " 3 by string desc" << endl << " -1 quit" << endl;
    cin >> i;
    if (i == -1) break;

    switch(i%4)
    {
    case 0:
      l.sort(CBIA());
      break;
    case 1:
      l.sort(CBID());
      break;
    case 2:
      l.sort(CBSA());
      break;
    case 3:
      l.sort(CBSD());
      break;
    }

    for (list<my_struct>::iterator it = l.begin(); it != l.end(); ++it)
    {
      cout << "{" << it->i << "," << it->s << "}" << endl;
    }
  }
}


这篇关于使用单个重载运算符对STL列表进行排序&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆