C++ day7

1、仿照vector手动实现自己的myVector,最主要实现二倍扩容功能

#include 
using namespace std;
 
  
//使用模板类
template
class myvector
{
private:
    T *first;
    T *last;
    T *end;
public:
    //无参构造
    myvector()
    {
        first = last = end = nullptr;
        cout<< "无参构造"< 
  
    }
    //有参构造
    myvector(int n, const T &val)
    {
        first = new T[n];
        for(int i = 0; i 
  
        {
            first[i] = val;
        }
        last = first + n;
        end = first + n;
        cout<<"有参构造"< 
  
    }
 
  
    //拷贝构造函数
    myvector(const myvector &other):first(new T(*(other.first))),last(new T(*(other.last))), end(new T(*(other.end)))
    {
        cout<<"拷贝构造函数"< 
  
    }
 
  
    //拷贝赋值函数
    myvector &operator=(const myvector &other)
    {
        int len = other.last-other.first;
        int size = other.end-other.first;
        first = new T[size];
        for(int i =0; i 
  
        {
            first[i] = other.first[i];
        }
        last = first + len;
        end = first +size;
 
  
        cout<<"拷贝赋值函数"< 
  
        return *this;
 
  
    }
 
  
    //析构函数
    ~myvector()
    {
        delete []first;
        first = nullptr;
        last = nullptr;
        end = nullptr;
        cout<<"析构函数"< 
  
    }
 
  
    //at函数
    T &at(int index)
    {
        //判断index是否在范围内
        if(index<0 && index>last-first)
        {
            cout<<"下标超出范围"< 
  
            throw(-1);
        }
        else
        {
            return first[index];
        }
    }
 
  
    //判空函数
    bool empty()
    {
        return first == last;
    }
 
  
    //判满函数
    bool full()
    {
        return last == end;
    }
 
  
    //返回第一个元素的函数
    T front()
    {
        return *first;
    }
 
  
    //返回最后一个元素的函数
    T back()
    {
        return *(last-1);
    }
 
  
    //返回表中元素个数的函数
    int size()
    {
        return (last-first);
    }
 
  
    //二倍扩容函数
    void expand()
    {
        int len = end-first;
        T *temp = new T(2*len);
        memcpy(temp, first, sizeof(T)*len);
        delete []first;
        first = temp;
        last = first+len;
        end = first + 2*len;
    }
 
  
    //在末尾添加一个元素
    void push_back(const T e)
    {
        //判满
        if(full()==true)
        {
            cout<<"链表已满,添加失败"< 
  
            cout<<"二倍扩容"< 
  
            expand();
        }
        *last = e;
        last++;
        cout<<"插入成功"< 
  
    }
 
  
    //删除最后一个元素函数
    void pop_back()
    {
        //判空
        if(empty()==true)
        {
            cout<<"链表为空,删除失败"< 
  
        }
        last--;
        cout<<"删除成功"< 
  
    }
 
  
    //删除所有元素函数
    void clear()
    {
        while(last != first)
        {
           // pop_back()函数删除当前最末的一个元素
            pop_back();
        }
    }
 
  
 
  
 
  
 
  
 
  
 
  
};
 
  
int main()
{
    myvector c1(2,'a');
    myvector c2(c1);
    myvector c3;
    c3 = c1;
 
  
    c1.push_back('b');
    c1.push_back('c');
    c1.push_back('d');
 
  
    cout< 
  
    cout< 
  
    cout< 
  
    cout<<"表中元素个数为:"< 
  
    cout<<"第一个元素为:"< 
  
    cout<<"最后一个元素为:"< 
  
 
  
    //调用在末尾删除函数
    c1.pop_back();
    cout<<"表中元素个数为:"< 
  
    //清空
    c1.clear();
    cout<<"表中元素个数为:"< 
  
 
  
    return 0;
}
 
  

2、思维导图

C++ day7_第1张图片

你可能感兴趣的:(c++,java,开发语言)