C++ 简单实现vectors

#ifndef M_VECTORS_H
#define M_VECTORS_H

template<typename T>
class My_Vectors
{
private:
    int m_len;
    int m_size;
    T* m_vectors;

public:
    My_Vectors() {}
    My_Vectors(int len, T val);
    My_Vectors(const My_Vectors &obj);

    void assign(int len, T &val);
    T& at(int index);
    T& back();
    int capacity()const;
    void clear();
    bool empty();
    T& front();
    void insert(int index, T val);
    int max_size() const;
    void pop_back();
    void push_back(const T &val);
    int size()const;
    void swap(My_Vectors &obj)const;

    bool operator != (const My_Vectors& obj)const;
    bool operator == (const My_Vectors& obj)const;
    T& operator[](int index);
    T operator[](int index) const;

    ~My_Vectors();
};

template<typename T>
My_Vectors<T>::My_Vectors(int len, T val)
{
    m_vectors = new T[len];
    for(int i = 0; i < len; i++)
    {
        m_vectors[i] = val;
    }

    m_len = len;
    m_size = len;
}

template<typename T>
My_Vectors<T>::My_Vectors(const My_Vectors &obj)
{
    m_len = obj.m_len;
    m_size = obj.m_size;

    m_vectors = new T[obj.m_len];

    for(int i = 0; i < obj.m_len; i++)
    {
        m_vectors[i] = obj.m_vectors[i];
    }
}

template<typename T>
void My_Vectors<T>::assign(int len, T &val)
{
    if(len > m_len)
    {
        T* pointer = new T[m_len*2];

        for(int i = 0; i < len; i++)
        {
            pointer[i] = val;
        }

        m_size = len;
        m_len *= 2;
        delete[] m_vectors;
        m_vectors = pointer;
    }
    else
    {
        T* pointer = new T[m_len];

        for(int i = 0; i < len; i++)
        {
            pointer[i] = val;
        }

        m_size = len;
        delete[] m_vectors;
        m_vectors = pointer;
    }

}

template<typename T>
T& My_Vectors<T>::at(int index)
{
    if(index >= m_len)
    {
        throw int(-1);
    }
    return m_vectors[index];
}

template<typename T>
T& My_Vectors<T>::back()
{
    return m_vectors[m_size];
}

template<typename T>
int My_Vectors<T>::capacity()const
{
    return m_len;
}

template<typename T>
void My_Vectors<T>::clear()
{
    delete[] m_vectors;
    m_vectors = new T[m_len];
    m_size = 0;
}

template<typename T>
bool My_Vectors<T>::empty()
{
    return (m_size) ? true : false;
}

template<typename T>
T& My_Vectors<T>::front()
{
    return m_vectors[0];
}

template<typename T>
void My_Vectors<T>::insert(int index, T val)
{
    if(index >= m_len)
    {
        T* pointer = new T[m_len*2];
        m_len *= 2;
        memcpy(pointer, m_vectors, sizeof(T)*m_len);
        if(index >= m_size)
        {
            m_vectors[index - 1] = val;
        }
        m_size++;
    }
    else
    {
       for(int i = m_size; i >= index; i--)
       {
           m_vectors[i] = m_vectors[i-1];
       }
       m_vectors[index - 1] = val;
       m_size++;
    }

}

template<typename T>
int My_Vectors<T>::max_size() const
{
    return m_len;
}

template<typename T>
void My_Vectors<T>::pop_back()
{
    m_size--;
    m_len--;
}

template<typename T>
void My_Vectors<T>::push_back(const T &val)
{
    if(m_size == m_len)
    {
        T* pointer = new T[m_len*2];
        m_len *= 2;

        memcpy(pointer, m_vectors, sizeof(T)*m_len);

        delete[] m_vectors;
        m_vectors = pointer;
    }

    m_vectors[m_size - 1] = val;
    m_size++;
}

template<typename T>
T& My_Vectors<T>::operator[](int index)
{
    return m_vectors[index];
}

template<typename T>
T My_Vectors<T>::operator[](int index) const
{
    return m_vectors[index];
}

template<typename T>
int My_Vectors<T>::size()const
{
    return m_size;
}

template<typename T>
void My_Vectors<T>::swap(My_Vectors &obj)const
{
    if(obj.m_len > m_len)
    {
        T* pointer = new T[m_len*2];
        int temp = obj.m_size;
        m_len = 2 * m_len;

        memcpy(pointer, obj.m_vectors, sizeof(T)*obj.m_len);

        delete[] obj.m_vectors;
        obj.m_vectors = m_vectors;
        obj.m_size = m_size;

        m_vectors = pointer;
        m_size = temp;
    }
    else if(obj.m_len < m_len)
    {
        T* pointer = new T[obj.m_len*2];
        int temp = m_size;
        obj.m_len *= 2;

        memcpy(pointer, m_vectors, sizeof(T)*m_len);

        delete[] m_vectors;
        m_vectors = obj.m_vectors;
        m_size = obj.m_size;

        obj.m_vectors = pointer;
        obj.m_size = temp;
    }
    else
    {
        T* pointer = new T[obj.m_len];
        memcpy(pointer, m_vectors, sizeof(T)*m_len);
        delete[] m_vectors;
        m_vectors = obj.m_vectors;

        int temp = m_size;
        m_size = obj.m_size;
        obj.m_size = temp;
    }
}

template<typename T>
bool My_Vectors<T>::operator == (const My_Vectors& obj)const
{
    if(m_size == obj.m_size)
    {
        return (memcmp(m_vectors, obj.m_vectors, sizeof(T)*m_size) == 0) ? 1 : 0;
    }
    return 0;
}

template<typename T>
bool My_Vectors<T>::operator != (const My_Vectors& obj)const
{
    if(m_size == obj.m_size)
    {
        return (memcmp(m_vectors, obj.m_vectors, sizeof(T)*m_size) == 0) ? 0 : 1;
    }
    return 1;
}

template<typename T>
My_Vectors<T>::~My_Vectors()
{
    delete[] m_vectors;
}

#endif // M_VECTORS_H

你可能感兴趣的:(c++)