• 类模板排序公共类设计


    Person.hpp

    #pragma once
    #include <string>
    template <class T>
    class Person
    {
        friend bool operator <(const Person& left, const Person& right)
        {
            return left.mAge<right.mAge ? true : false;
        }
        friend bool operator >(const Person& left, const Person& right)
        {
            return left.mAge>right.mAge ? true : false;
        }
    public:
        Person(){}
        Person(std::string name, int age) {
            this->mName = name;
            this->mAge = age;
        }
        ~Person() {};
    public:
        T mName;
        int mAge;
    };

    MyArray.hpp

    #pragma once
    template <class T>
    class MyArray
    {
    public:
        explicit MyArray(int capacity)
        {
            this->m_Capacity = capacity;
            this->m_Size = 0;
            this->pAddress = new T[this->m_Capacity];
        }
    
        MyArray(const MyArray& arr)
        {
            this->m_Capacity = arr.m_Capacity;
            this->m_Size = arr.m_Size;
            this->pAddress = new T[arr.m_Capacity];
            for (int i = 0; i < arr.m_Size; i++)
            {
                this->pAddress[i] = arr.pAddress[i];
            }
        }
    
        T& operator[](int index)
        {
            return this->pAddress[index];
        }
    
        void Push_back(const T& val)
        {
            if (this->m_Capacity == this->m_Size)
            {
                return;
            }
            this->pAddress[this->m_Size] = val;
            this->m_Size++;
        }
    
        void Pop_back()
        {
            if (this->m_Size == 0)
            {
                return;
            }
            this->m_Size--;
        }
    
        int getSize()
        {
            return this->m_Size;
        }
    
        ~MyArray()
        {
            if (this->pAddress != nullptr)
            {
                delete[] this->pAddress;
                this->pAddress = nullptr;
                this->m_Capacity = 0;
                this->m_Size = 0;
            }
        }
    private:
        T* pAddress;
        int m_Capacity;
        int m_Size;
    };

     排序模板方法

    template<class T>
    bool ascend(T& a, T& b)
    {
        return a < b ? true : false;
    }
    
    template<class T>
    bool descend(T& a, T& b)
    {
        return a > b ? true : false;
    }
    
    template <class T>
    class Test
    {
    public:
        static void Sort(T* array, int len, bool(*compare)(T& a, T& b))
        {
            T Temp;
            assert(len >= 1);
            int exchange = 0;
            for (int i = 0; i < len - 1; i++)
            {
                exchange = 0;
                for (int j = len - 1; j >= i; j--)
                {
                    if (compare(array[j], array[j - 1]))
                    {
                        Temp = array[j - 1];
                        array[j - 1] = array[j];
                        array[j] = Temp;
                        exchange = 1;
                    }
                }
                if (exchange != 1)
                {
                    return;
                }
            }
        }
    };

    Test

    void Test02()
    {
        MyArray<Person<string>> myArrayPerson(10);
        Person<string> p1("张飞", 25);
        Person<string> p2("马超", 23);
        Person<string> p3("赵云", 24);
        Person<string> p4("关羽", 28);
        Person<string> p5("黄忠", 76);
        myArrayPerson.Push_back(p1);
        myArrayPerson.Push_back(p2);
        myArrayPerson.Push_back(p3);
        myArrayPerson.Push_back(p4);
        myArrayPerson.Push_back(p5);
        PrintPerson(myArrayPerson);
        cout << "------------------------------------" << endl;
    }
    
    void Test04()
    {
        int arrTest[] = { 1,2,5,3,6,3,7,9,8 };
        int nLen = sizeof(arrTest) / sizeof(arrTest[0]);
        Test<int>::Sort(arrTest, nLen, ascend);
        for (int i = 0; i < nLen; i++)
        {
            cout << i << " ";
        }
        cout << endl;
    }
    
    void Test05()
    {
    
        Person<string> p1("张飞", 25);
        Person<string> p2("马超", 23);
        Person<string> p3("赵云", 24);
        Person<string> p4("关羽", 28);
        Person<string> p5("黄忠", 76);
    
        Person<string> pArr[] = { p1 ,p2 ,p3,p4,p5 };
        int nLen = sizeof(pArr) / sizeof(pArr[0]);
        Test<Person<string>>::Sort(pArr, nLen, ascend);
        for (int i = 0; i < nLen; i++)
        {
            cout << "name: " << pArr[i].mName << " age: " << pArr[i].mAge << endl;
        }
        cout << endl;
    }
    
    void Test06()
    {
        MyArray<Person<string>> myArrayPerson(10);
        Person<string> p1("张飞", 25);
        Person<string> p2("马超", 23);
        Person<string> p3("赵云", 24);
        Person<string> p4("关羽", 28);
        Person<string> p5("黄忠", 76);
        myArrayPerson.Push_back(p1);
        myArrayPerson.Push_back(p2);
        myArrayPerson.Push_back(p3);
        myArrayPerson.Push_back(p4);
        myArrayPerson.Push_back(p5);
        PrintPerson(myArrayPerson);
        cout << "------------------------------------" << endl;
    
        int nLen = myArrayPerson.getSize();
        Test<Person<string>>::Sort(&myArrayPerson[0], nLen, ascend);
        for (int i = 0; i < nLen; i++)
        {
            cout << "name: " << myArrayPerson[i].mName << " age: " << myArrayPerson[i].mAge << endl;
        }
        cout << endl;
    }

     test02

    test04

    test05

    test06

  • 相关阅读:
    iPhone开发:iPad的自定义菜单项研究
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address
    nginx重启报找不到nginx.pid的解决方法
    解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
    LNMP安装了哪些软件?安装目录在哪?
    SSL和SSH有什么区别
    CLR Via CSharp读书笔记(16):数组
    ubuntu server 下部署apache+django+mod_wsgi环境
    Ubuuntu10.04上配置Django、Apache、mod_wsgi
    Tutorial: Installing Django 1.1. on CentOS 5.4
  • 原文地址:https://www.cnblogs.com/mmc9527/p/10455653.html
Copyright © 2020-2023  润新知