zoukankan      html  css  js  c++  java
  • *** 实现冒泡排序模板

    #include <iostream>
    using namespace std;
    
    template<class T>
    bool increSort(T &a, T &b)
    {
        return (a > b);
    }
    
    template<class T>
    bool decreSort(T &a, T &b)
    {
        return (a < b);
    }
    
    template<class T>
    void Sort(T* array, int len, bool(*compare)(T&, T&))
    {
        T temp;
        for (int i = 0; i < len - 1; i++)
        {
            for (int j = len - 1; j > i; j--)
            {
                if (compare(array[j-1], array[j]))
                {
                    temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                }
            }
        }
    }
    
    template<class T>
    class Rec
    {
    public:
        Rec(T a = 0, T b = 0) :length(a), width(b) {}
        T Area()
        {
            return length * width;
        }
    
    private:
        T length;
        T width;
    };
    
    template<class T>
    bool operator > (Rec<T> & a, Rec<T> &b)
    {
        return (a.Area() > b.Area());
    }
    
    template<class T>
    bool operator < (Rec<T> & a, Rec<T> &b)
    {
        return (a.Area() < b.Area());
    }
    
    int main()
    {
        Rec<double> a[] =
        {
            Rec<double>(1.1, 5.2),
            Rec<double>(3.3, 7.8),
            Rec<double>(4.4,2.8),
            Rec<double>(6.1,1.5)
        };
        int size = sizeof(a) / sizeof(Rec<double>);
    
        cout << "size is " << size << endl;
    
        Sort(a, size, increSort);
        
        for (int i = 0; i < size; i++)
        {
            cout << a[i].Area() << endl;
        }
    
    
        int i;
        cin >> i;
    
        return 0;
    }
  • 相关阅读:
    转载 | CSS文本溢出显示省略号
    转载 | CSS书写顺序
    转载 | CSS布局大全
    threejs sprite 制作标签
    typeScript 中的类
    Es5中的类
    typeScript中的函数
    websocket在vue项目中的使用
    typeScript中的变量数据类型
    echarts 中的1/4圆环行图的使用
  • 原文地址:https://www.cnblogs.com/superrunner/p/10171957.html
Copyright © 2011-2022 走看看