自定义vector模板类

template <typename T>

class Myvector

{

private:

T * first;

T * last;

T * end;

};

要实现的函数:

构造函数 析构函数 拷贝构造 拷贝赋值 at() empty() full() front() back() size() clear() expand() 二倍扩容函数 push_back() pop_back()

头文件

#ifndef MYVECTOR_H
#define MYVECTOR_H
#include <iostream>
using namespace std;
template<typename T>
class Myvector
{
public:
    Myvector():first(nullptr),last(nullptr),end(nullptr) {};
    Myvector(Myvector const &other) ;
    Myvector& operator=(Myvector const &other);
    ~Myvector();
    T& at(int pos);
    bool empty()const ;
    bool full()const;
    T& front();
    T& back();
    int size() const;
    int capacity() const;
    void clear();
    void expand();
    void push_back(const T &val);
    void pop_back();

private:
    T *first;
    T *last;
    T *end;

};
//拷贝构造
template<typename T>
Myvector<T>:: Myvector(Myvector const &other):
    first(nullptr),last(nullptr),end(nullptr)
{
   if(!other.empty())
       return;
   first = new T[other.size()];
   last = first + other.size();
   for(int i = 0; i < other.size(); i++)
   {
       *(first + i) = *(other.first + i);
   }

}
//拷贝赋值
template <typename T>
Myvector<T>& Myvector<T>::operator=(const Myvector& other)
{
    if (this == &other) {
        return *this;
    }
    clear();
    if (!other.empty()) {
        first = new T[other.size()];
        end = first + other.size();
        last = first + other.size();
        for (int i = 0; i < other.size(); ++i) {
            *(first + i) = *(other.first + i);
        }
    }
    return *this;
}


template<typename T>
Myvector<T>:: ~Myvector()
{
    clear();
}
//at
template <typename T>
T& Myvector<T>::at(int index)
{
    if (index < 0 || index >= size()) {
        throw std::out_of_range("Invalid index!");
    }
    return *(first + index);
}

template <typename T>
bool Myvector<T>::empty() const {
    return size() == 0;
}

template <typename T>
bool Myvector<T>::full() const
{
    return size() == capacity();
}

// 返回首元素的引用
template <typename T>
T& Myvector<T>::front()
{
   return *first;
}

// 返回尾元素的引用
template <typename T>
T& Myvector<T>::back()
{
    return *(last - 1);
}

// 返回容器大小
template <typename T>
int Myvector<T>::size() const {
    return last - first;
}

// 清空容器
template <typename T>
void Myvector<T>::clear() {
    last = first;
}

template <typename T>
void Myvector<T>::expand()
{
    int capacity =end-first;//计算当前容量
    T *new_first=new T[capacity *2];//分配新的空间
    T *new_last=new_first+size();//将原有数据偏移
    //复制原有的数据
    for(int i=0;i<size();++i)
    {
        first[i]=first[i];
    }
    delete[] first;//释放原有空间
    first =new_first;
    last =new_last;
    end=first+capacity *2;
}

// 在末尾添加一个元素
template <typename T>
void Myvector<T>::push_back(const T& value) {
    if (full())
        expand();

    *last = value;
    last++;
}

// 移除末尾的元素
template <typename T>
void Myvector<T>::pop_back() {
    if (empty())
        return;

    last--;
}

// 返回容器的容量
template <typename T>
int Myvector<T>:: capacity() const {
    return end - first;
}

#endif // MYVECTOR_H

主函数文件

#include <iostream>
#include "myvector.h"
using namespace std;

int main()
{
    Myvector<int> v1;
    cout << "sizeof(v1) = " << sizeof(v1) << endl;
    cout <<"empty="<< v1.empty() << endl;
    for(int i = 0; i < 20; i++)
    {
        v1.push_back(i);
    }
    cout <<"empty="<< v1.empty() << endl;
    cout <<"size="<< v1.size() << endl;
    cout <<"full="<< v1.full() << endl;



    v1.at(v1.size() - 1) = 40;
    for(int i = 0; i < 20; i++)
    {
        cout << v1.at(i) << " ";
    }
    cout << endl;
    cout <<"front="<< v1.front() << endl;
    cout <<"back="<< v1.back() << endl;
    v1.pop_back();
    cout <<"size="<< v1.size() << endl;
    v1.clear();

    cout <<"empty="<< v1.empty() << endl;
    cout <<"size="<< v1.size() << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值