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

其实本质就是适配库函数sort

c++ 想要适配sort 需要

1、适配随机随机迭代器

2、提供begin(), end() 函数

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

cpp 复制代码
 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;
    }
复制代码
相关推荐
风清扬_jd5 分钟前
Chromium 中JavaScript Fetch API接口c++代码实现(二)
javascript·c++·chrome
huapiaoy10 分钟前
Redis中数据类型的使用(hash和list)
redis·算法·哈希算法
冷白白23 分钟前
【C++】C++对象初探及友元
c语言·开发语言·c++·算法
鹤上听雷32 分钟前
【AGC005D】~K Perm Counting(计数抽象成图)
算法
睡觉然后上课41 分钟前
c基础面试题
c语言·开发语言·c++·面试
一叶祇秋44 分钟前
Leetcode - 周赛417
算法·leetcode·职场和发展
qing_0406031 小时前
C++——继承
开发语言·c++·继承
武昌库里写JAVA1 小时前
【Java】Java面试题笔试
c语言·开发语言·数据结构·算法·二维数组
ya888g1 小时前
GESP C++四级样题卷
java·c++·算法
小叶学C++1 小时前
【C++】类与对象(下)
java·开发语言·c++