C++实现模板数组

2022-10-26  本文已影响0人  二进制人类

myarray.hpp

#ifndef MYARRAY_HPP
#define MYARRAY_HPP
#include <iostream>
#include <string.h>
using namespace std;
template<class T>
class MyArray
{
    template<typename T1>
    friend ostream& operator<<(ostream &out, MyArray<T1> &ob);
private:
    T *arr;
    int size;
    int capacity;//容量
public:
    MyArray();
    MyArray(int capacity);

    //指针成员必须实现的函数
    MyArray(const MyArray<T> &ob);
    ~MyArray();
    MyArray& operator=(const MyArray &ob);

    //功能函数
    void push_back(T elem);//尾部插入元素
    void pop_back();//尾部删除元素
    T& at(int pos);//取数组的元素
    void sort();//排序


};

template<class T>
MyArray<T>::MyArray()
{
    size = 0;
    capacity=5;

    //获取数组的堆区空间
    arr=new T[capacity];
    //将数组空间 清0
    memset(arr, 0, sizeof(T)*capacity);
}

template<class T>
MyArray<T>::MyArray(int capacity)
{
    size = 0;
    this->capacity=capacity;

    //获取数组的堆区空间
    arr=new T[capacity];
    //将数组空间 清0
    memset(arr, 0, sizeof(T)*capacity);
}

template<class T>
MyArray<T>::MyArray(const MyArray<T> &ob)
{
    cout<<"-----------------"<<endl;
    size=ob.size;
    capacity = ob.capacity;

    //获取数组的堆区空间
    arr=new T[capacity];
    //将数组空间 清0
    memset(arr, 0, sizeof(T)*capacity);

    //将ob数组中元素拷贝过来
    memcpy(arr,ob.arr, sizeof(T)*capacity);
}

template<class T>
MyArray<T>::~MyArray()
{
    if(arr != NULL)
    {
        delete [] arr;
        arr=NULL;
    }
}

template<class T>
MyArray<T> &MyArray<T>::operator=(const MyArray &ob)
{
    //ob2 = ob1;//this代表ob2  ob代表ob1
    //判断ob2是否有指向
    if(arr != NULL)
    {
        delete [] arr;
        arr=NULL;
    }

    size = ob.size;
    capacity = ob.capacity;

    //获取数组的堆区空间
    arr=new T[capacity];
    //将数组空间 清0
    memset(arr, 0, sizeof(T)*capacity);

    //将ob数组中元素拷贝过来
    memcpy(arr,ob.arr, sizeof(T)*capacity);

    return *this;
}

template<class T>
void MyArray<T>::push_back(T elem)
{
    //判断容器是否满
    if(size == capacity)
    {
        //扩容器
        T *tmp = new T[2*capacity];
        memset(tmp,0,sizeof(T)*2*capacity);

        //将原来的数组数据 拷贝过来
        memcpy(tmp,arr,sizeof(T)*capacity);

        //释放原来的空间
        delete [] arr;

        //更新arr的指向
        arr = tmp;

        //更新容量大小
        capacity = 2*capacity;
    }


    arr[size] = elem;
    size++;

    return;
}

template<class T>
void MyArray<T>::pop_back()
{
    //判断容器是否为空
    if(size == 0)
    {
        cout<<"容器没有元素 不允许删除"<<endl;
        return;
    }
    size--;
    return;
}

template<class T>
T &MyArray<T>::at(int pos)
{
    //判断pos有效性
    if(pos<0 || pos >=size)
    {
        cout<<pos<<"位置不合法"<<endl;
        exit(-1);
    }

    return arr[pos];
}

template<class T>
void MyArray<T>::sort()
{
    if(size == 0)
    {
        cout<<"容器为空 不能排序"<<endl;
        return;
    }

    int i=0;
    for(i=0;i<size-1;i++)
    {
        int j = 0;
        for(j=0;j<size-i-1;j++)
        {
            if(arr[j] > arr[j+1])
            {
                T tmp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1]=tmp;
            }
        }
    }
    return;
}

#endif // MYARRAY_HPP

main.cpp

#include <iostream>
#include <string>
#include "myarray.hpp"
using namespace std;

class Person
{
    friend ostream& operator<<(ostream &out, Person ob);
private:
    int num;
    string name;
public:
    Person(){}
    Person(int num, string name)
    {
        this->num = num;
        this->name = name;
    }
    //重载>
    bool operator>(const Person &ob)
    {
        return num>ob.num;
    }
};

ostream& operator<<(ostream &out, Person ob)
{
    out<<ob.num<<" "<<ob.name<<" ";
    return out;
}

template<typename T1>
ostream& operator<<(ostream &out, MyArray<T1> &ob)
{
    cout<<ob.size<<endl;
    int i=0;
    for(i=0;i<ob.size;i++)
    {
        out<<ob.arr[i]<<" ";
    }
    out<<endl;

    return out;
}
int main(int argc, char *argv[])
{
    MyArray<int> arr1;
    arr1.push_back(20);
    arr1.push_back(30);
    arr1.push_back(50);
    arr1.push_back(10);
    arr1.push_back(40);
    cout<<arr1<<endl;
    arr1.sort();
    cout<<arr1<<endl;

    MyArray<Person> arr2;
    arr2.push_back(Person(103,"lucy"));
    arr2.push_back(Person(101,"bob"));
    arr2.push_back(Person(105,"tom"));
    arr2.push_back(Person(104,"Harry"));

    cout<<arr2<<endl;
    arr2.sort();
    cout<<arr2<<endl;

    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读