嵌套模板类

嵌套模板类


#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <vld.h>
using namespace std;

template <class T>
class Stack
{
private:
    T* items;
    int tt;
    int stackSize;
public:
    Stack(int _size=3) : stackSize(_size), tt(0)
    {
        items = new T[stackSize + 1];
    }
    ~Stack()
    {
        delete[] items; items = nullptr;
    }
    // 如果我们被Vector嵌套,那么扩展外层Vector时候会遇到将一个Stack类赋值给另一个Stack类
    //      的情况,所以必须重载赋值运算符,防止浅拷贝的出现,进而防止操作野指针
    Stack& operator=(const Stack& ss)
    {
        if (this == &ss) return *this;
        delete[] items;
        stackSize = ss.stackSize;
        items = new T[stackSize + 1];
        for (int i = 0; i <= stackSize; i++) items[i] = ss.items[i];
        tt = ss.tt;
        return *this;
    }
    bool empty() const
    {
        return tt == 0;
    }
    bool full() const
    {
        return tt == stackSize;
    }
    void push(const T& value)
    {
        items[++tt] = value;
    }
    const T& top()
    {
        return items[tt];
    }
    const T& pop()
    {
        tt--;
    }
};


template <class T>
class Vector
{
private:
    // 数组长度
    int len;
    // 数组元素
    T* items;
public:
    Vector(int _len=2) : len(_len)
    {
        items = new T[len];
    }
    Vector(const Vector& vv) : len(vv.len)
    {
        items = new T[len];
        for (int i = 0; i < len; i++) items[i] = vv.items[i];
    }
    ~Vector()
    {
        delete[] items; items = nullptr;
    }
    // 扩展数组长度
    void resize(int new_len)
    {
        // 扩展后的长度不如原长度长,直接返回 
        if (new_len <= len) return; 
        T* tmp = new T[new_len];
        // 将原数组的元素复制到新数组中
        // 如果我们要嵌套Vector,那么扩展外层Vector时候会遇到将一个Vector类赋值给另一个Vector类
        //      的情况,所以必须重载赋值运算符,防止浅拷贝的出现,进而防止操作野指针
        for (int i = 0; i < len; i++) tmp[i] = items[i];
        // 释放原数组的内存空间
        delete[] items;
        // 将新数组指针复制到原数组上
        items = tmp;
        len = new_len;
    }
    int size()
    {
        return len;
    }
    Vector& operator=(const Vector& vv)
    {
        delete[] items;
        len = vv.len;
        items = new T[len];
        for (int i = 0; i < len; i++) items[i] = vv.items[i];
        return *this;
    }
    // 重载下标运算符
    T& operator[] (int index)
    {
        // 如果我们操作的下标已经超过了数组的长度,我们自动扩展数组的长度
        if (index >= len) resize(index + 1);
        return items[index];
    }
    const T& operator[] (int index) const
    {
        return items[index];
    }
};

int main()
{
    Vector<Stack<string>> vs;
    vs[0].push("diaochan"), vs[1].push("hanxin");
    vs[2].push("zzz"), vs[3].push("zirui");
    cout << vs[3].top() << endl;

    Vector<Vector<int>> vv;
    vv[0][0] = 1, vv[0][1] = 13, vv[0][2] = 8;
    vv[2][0] = 81, vv[2][1] = 36, vv[2][2] = 28;
    for (int i = 0; i < 3; i++) cout << vv[2][i] << ' ';
    cout << endl;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值