c++自定义数据结构适配std::sort

其实本质就是适配库函数sort
c++ 想要适配sort 需要
1、适配随机随机迭代器
2、提供begin(), end() 函数

下面的代码看起来很复杂, 其实主要是需要适配的接口稍多,核心代码很简单

 template<typename Derived>
    class EqualityComparable {
    public:
        friend bool operator==(Derived const& x1, Derived const& x2) {
            return !(x1 < x2) && !(x2 < x1);
        }

        friend bool operator!=(Derived const& x1, Derived const& x2) {
            return !(x1 == x2);
        }

        friend bool operator>(Derived const& x1, Derived const& x2) {
            return x2 < x1;
        }

        friend bool operator>=(Derived const& x1, Derived const& x2) {
            return !(x1 < x2);
        }

        friend bool operator<=(Derived const& x1, Derived const& x2) {
            return !(x1 > x2);
        }
    };


    class DiagIte : public EqualityComparable<DiagIte> {
    private:
        std::vector<std::vector<int>>& mat;
        int lx, ly;

    public:
        using difference_type = int;
        using value_type = int;
        using pointer = int*;
        using reference = int&;
        using iterator_category = std::random_access_iterator_tag;

        DiagIte(std::vector<std::vector<int>>& mat, int lx, int ly) : mat(mat), lx(lx), ly(ly) {}

        DiagIte(const DiagIte& other) : mat(other.mat), lx(other.lx), ly(other.ly) {}

        DiagIte& operator=(const DiagIte& other) {
            if (this != &other) {
                mat = other.mat;
                lx = other.lx;
                ly = other.ly;
            }
            return *this;
        }

        reference operator*() const {
            return mat[lx][ly];
        }

        DiagIte& operator++() {
            ++lx;
            ++ly;
            return *this;
        }

        DiagIte operator++(int) {
            DiagIte temp = *this;
            ++(*this);
            return temp;
        }

        DiagIte& operator--() {
            --lx;
            --ly;
            return *this;
        }

        DiagIte operator--(int) {
            DiagIte temp = *this;
            --(*this);
            return temp;
        }

        DiagIte& operator+=(difference_type n) {
            lx += n;
            ly += n;
            return *this;
        }

        DiagIte& operator-=(difference_type n) {
            lx -= n;
            ly -= n;
            return *this;
        }

        DiagIte operator+(difference_type n) const {
            DiagIte result = *this;
            result += n;
            return result;
        }

        DiagIte operator-(difference_type n) const {
            DiagIte result = *this;
            result -= n;
            return result;
        }

        difference_type operator-(const DiagIte& other) const {
            return (lx - other.lx);
        }

        reference operator[](difference_type n) const {
            return *(*this + n);
        }

        bool operator<(const DiagIte& other) const {
            return (lx < other.lx);
        }
    };

    class Diag {
    public:
        vector<vector<int>>& mat;
        int lx, ly;

        Diag(vector<vector<int>>& mat, int lx, int ly) : mat(mat), lx(lx), ly(ly) {}

        DiagIte begin() {
            return DiagIte(mat, lx, ly);
        }

        DiagIte end() {
            int w = std::min(mat.size() - lx, mat[0].size() - ly);
            return DiagIte(mat, lx + w, ly + w);
        }
    };

    vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
        int m = mat.size(), n = mat[0].size();
        for (int i = 0; i < mat.size(); i++) {
            Diag Di(mat, i, 0);
            std::sort(Di.begin(), Di.end());
        }
        for (int j = 1; j < mat[0].size(); j++) {
            Diag Di(mat, 0, j);
            std::sort(Di.begin(), Di.end());
        }
        return mat;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值