【问题标题】:c++ data structure that automatically sort objects by the value of a memberc++ 数据结构,根据成员的值自动对对象进行排序
【发布时间】:2015-12-27 18:19:22
【问题描述】:

在 STL 中,列表是一种数据结构,可以自动按数值对数字进行排序。如果元素不是数字而是类的实例,并且我希望容器根据类成员的值自动对元素进行排序,我应该使用什么样的容器?例如

class Rect{
    double height;
    double width;
    double area;
};

我希望容器按矩形的area 自动排序。

【问题讨论】:

  • "在 STL 中,列表是一种数据结构,可以自动按数值对数字进行排序。"真的吗? std::list 不会做这种事。 “STL”是什么意思?
  • STD 是标准模板库的过时首字母缩写词。否 std::list 不会自动对元素进行排序。为此,您需要std:set

标签: c++ stl priority-queue


【解决方案1】:

您有 std::multiset 用于自动订购容器:

std::multiset<Rect, LessArea> rects;

LessArea

struct LessArea
{
    bool operator ()(const Rect& lhs, const Rect& rhs) const
    {
        return lhs.area < rhs.area;
    }
};

【讨论】:

    【解决方案2】:

    stl::priority_queue 是你想要的。只需使用less&lt;Rect&gt;Rects 上定义一个排序,或者将stl::priority_queue 专门用于Rect

    【讨论】:

      【解决方案3】:

      如果你想自动排序,你必须做所谓的

      1) Operator Overloading of less than < operator

      2) Comparator function

      对于您的任务,要自动排序,在您编写比较函数或重载

      STL setpriority queue 。这两个数据结构被定义为根据比较函数自动对元素进行排序。但这里需要注意的是,您不能在其中插入重复的元素 set ,也就是说,如果两个矩形的面积相同,那么这些矩形中的第一个将保存在集合中。第二个无法插入。

      【讨论】:

      • 您有std::multiset 允许重复
      【解决方案4】:

      使用 std::set 的程序如下所示。

      class Rect{
          double height;
          double width;
          double area;
      
      public:
          bool operator<( const Rect& rhs ) const
              { return area < rhs.area; }
      };
      

      您需要定义“

      【讨论】:

      • 您有std::multiset 允许重复
      【解决方案5】:

      我不介意使用矢量,特别是如果您希望能够存储重复元素。通过使用一个简单的 lambda 函数,我们可以按照我们想要的 Rect 的任何成员对对象进行排序。在本例中,我选择按区域对 Rect 对象进行排序。

      #include <iostream>
      #include <vector>
      #include <algorithm>
      using namespace std;
      class Rect {
          Rect(double height, double width, double area) {
              _height = height;
              _width = width;
              _area = area;
          }
      
          double area() const {
              return _area;
          }
      
          // sorts Rect objects by ascending order of area
          static void sort_by_area(vector<Rect> &shapes) {
              sort(shapes.begin(), shapes.end(), [](const Rect &first, const Rec &second)
                                                 { return first.area() < second.area); });
          }
      
      private:
          double _height;
          double _width;
          double _area;
      };
      
      int main() {
          vector<Rect> r;
          r.push_back(Rect(0,0,3));
          r.push_back(Rect(0,0,2));
          r.push_back(Rect(0,0,4));
      
          for(auto &obj : r) {
              //prints 3 2 4
              cout << obj.area() << endl;
          }
      
          Rect::sort_by_area(r);
      
          for(auto &obj : r) {
              //prints 2 3 4
              cout << obj.area() << endl;
          }
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-10-06
        • 1970-01-01
        • 2020-04-24
        • 2015-06-26
        • 1970-01-01
        • 2013-11-25
        • 1970-01-01
        • 2017-08-16
        • 2010-11-16
        相关资源
        最近更新 更多