C++-day(6)-(vector)全都是BUG 忽看

该文是关于C++编程的一个实现,创建了一个名为Myvector的模板类,模仿了标准库中的vector容器。类包含了常见的构造函数(无参、有参、拷贝构造)、赋值运算符、访问元素的方法(中括号[]、at函数)、容器状态检查(empty、full)、以及动态扩容、添加元素(push_back)、移除元素(pop_back)等功能。
摘要由CSDN通过智能技术生成

 错误代码*********

//#include <iostream>
//#include <cstring>
//#include<vector>

//using namespace std;

创建函数模板类
//template <typename T>
//class Myvector
//{
//private:
//    //指向表首
//    T *first;

//    //指向表尾的后一位
//    T *end;

//    //指向表尾
//    T *last;

//    //现有空间的大小
//    int len = 0;

//    int size_t = 0;

//public:

//    //无参构造
//    Myvector();

//    //有参构造函数
//    Myvector(int size = 1);

//    //析构函数
//    ~Myvector();

//    //拷贝构造
//    Myvector(const Myvector &other);

    拷贝赋值
    Myvector &operator = (const Myvector &other);

//    //中括号[]
//    T &operator [](const int index);

//    //at函数
//    T &at(int pos);

//    //empty函数
//    bool empty();

//    //full
//    bool full();

//    //front函数
//    T &front()const;

//    //back函数
//    T &back();

//    //size函数
//    int my_size();

//    //clear函数
//    void claer();

//    //二倍扩容
//    void expand();

//    //push_back
//    void push_back(const T e);

//    //pop_back
//    void pop_back(void);

    //遍历
    void soft();

//};

无参构造
//template <typename T>
//Myvector<T> ::Myvector():first(nullptr), end(nullptr), last(nullptr)
//{
//}

有参构造
//template <typename T>
//Myvector<T> ::Myvector(int size)
//{
//    first = new T[size];  //创建空间
//    last = first;
//    end = first + size;   //end指向尾部的后一位
//}

析构函数
//template <typename T>
//Myvector<T> ::~Myvector()
//{
//    //first不为空delete删除
//    if(nullptr != first)
//    {
//        delete []first;
//    }

//    //全部指向为空
//    first = end = last = nullptr;
//}

拷贝构造函数
//template <typename T>
//Myvector<T> ::Myvector(const Myvector &other)
//{
//    //计算原始空间大小
//    this -> len = other.last - other.first;

//    //创建新的容量
//    this -> first = new T[len];

//    //拷贝进入新的空间
//    memcpy(this -> first, other -> first, len * sizeof(T));

//    //指向新表首尾的位置
//    this -> last = this -> first + len;
//    this -> end = this -> last;
//}

拷贝赋值函数
template <typename T>
Myvector<T> &Myvector<T> ::operator = (const Myvector &other)
{
    if(this != &other)
    {
        this -> first = other.first;
        this -> end = other.end;
        this -> size_t = other.size_t;

    }
}

中括号[]
//template <typename T>
//T &Myvector<T> ::operator[](const int index)
//{
//    return *(first + index);
//}

at函数
//template <typename T>
//T &Myvector<T> ::at(int pos)
//{
//    if(pos < 0 && pos > last - first)
//    {
//        cout << "下标超出范围" << endl;
//    }
//    else
//    {
//       return first[pos];
//    }
//}

empty函数
//template <typename T>
//bool Myvector<T> ::empty()
//{
//    return first == last;
//}

full函数
//template <typename T>
//bool Myvector<T> ::full()
//{
//    return last == end;
//}

front函数
//template <typename T>
//T &Myvector<T> ::front()const
//{
//    //取得第一个首地址
//    return *first;
//}

back函数
//template <typename T>
//T &Myvector<T> ::back()
//{
//    //返回最后一个值
//    return *end;
//}

size函数
//template <typename T>
//int Myvector<T> ::my_size()
//{
//    return last - first;
//}

clear函数
//template <typename T>
//void Myvector<T> ::claer()
//{
//   if(empty() == 0)
//   {
//       cout << "数据为空" << endl;
//   }

//   while(last != first)
//   {
//       memset(begin, 0, sizeof(T) * (last - first + 1));

//   }
//}

二倍扩容
//template <typename T>
//void Myvector<T> ::expand()
//{
//    len = end - first;
//    T *temp = new T(2 * len);
//    memcmp(temp, first, sizeof(T) * len);
//    delete  []first;
//    first = temp;
//    last = first + len;
//    end = first + 2 * len;

//}

push_back
//template <typename T>
//void Myvector<T> ::push_back(const T e)
//{
//    if(full() == 0)
//    {
//        cout << "数据已满" << endl;
//        cout << "进行二倍扩容" << endl;
//        expand();
//    }

//    *last = e;
//    last++;
//}

pop_back
//template <typename T>
//void Myvector<T> ::pop_back(void)
//{
//    if(empty() == 0)
//    {
//        cout << "数据为空" << endl;
//        return;
//    }
//    last--;
//}

//遍历
template <typename T>
void Myvector<T> ::soft()
{
    for(int i = 0; i <= end - first; i++)
    {
        cout << last[i] << endl;
    }
}

//int main()
//{
//    Myvector<int> v(1);

//    for(int i = 0; i < 5; i++)
//    {
//        v.push_back(i);
//    }

//    cout << "v.at(1) = " << v.at(1) << endl;
//    cout << "front = " << v.front() << endl;
//    cout << "back = " << v.back() << endl;
//    cout << "pop_back " << v.pop_back();
         <<"  size = " << v.my_size() << endl;

//    return 0;
//}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值