鱼C论坛

 找回密码
 立即注册
查看: 1186|回复: 2

[已解决]C++有关模板的题

[复制链接]
发表于 2021-5-10 16:01:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

                               
登录/注册后可看大图
最佳答案
2021-5-12 14:54:03
本帖最后由 yuxijian2020 于 2021-5-12 15:02 编辑

工具人来了
#include <iostream>
#include <initializer_list>
#include <cassert>

template<class T>
class T_Counter
{
public:
        //////////////////    默认构造    //////////////////
        T_Counter() { m_data = new T(); }
        //////////////////    拷贝构造    //////////////////
        explicit T_Counter(T_Counter<T>& other)
        {
                m_data = new T();
                *m_data = *other.m_data;
        }
        explicit T_Counter(const T_Counter<T>& other)
        {
                m_data = new T();
                *m_data = *other.m_data;
        }
        //////////////////    移动构造    //////////////////
        explicit T_Counter(T_Counter<T>&& other)
        {
                m_data = other.m_data;
                other.m_data = nullptr;
        }
        explicit T_Counter(T&& data)
        {
                m_data = new T();
                *m_data = data;
        }
        //////////////////    单参数构造    //////////////////
        explicit T_Counter(T& data)
        {
                m_data = new T();
                *m_data = data;
        }
        //////////////////    析构    //////////////////
        ~T_Counter() { if (m_data) delete m_data; }

        //////////////////    重载运算符    //////////////////
        T_Counter<T>& operator=(const T_Counter<T>& other) { *m_data = *other.m_data; return *this; }
        T_Counter<T> operator+(T_Counter<T>& other) { return T_Counter<T>(*m_data + *other.m_data); }
        T_Counter<T> operator-(const T_Counter<T>& other) { return T_Counter<T>(*m_data - *other.m_data); }
        T_Counter<T>& operator+=(const T_Counter<T>& other) { *m_data += *other.m_data; return *this; }
        T_Counter<T>& operator-=(const T_Counter<T>& other) { *m_data -= *other.m_data; return *this; }

        //////////////////    重载友元流操作符    //////////////////
        friend std::ostream& operator<<(std::ostream& os, const T_Counter<T>& t) { os << *t.m_data; return os; }
        friend std::istream& operator>>(std::istream& is, const T_Counter<T>& t) { is >> *t.m_data; return is; }

        //////////////////    公开接口    //////////////////

        //设置数据内容
        void SetData(const T& data) { *m_data = data; }
        //获取数据内容
        T GetData() const { return *m_data; }

private:
        T* m_data;
};

template<class T>
class T_Vector
{
public:
        //////////////////    默认构造    //////////////////
        explicit T_Vector() : m_vec(nullptr), m_size(0), m_capicity(0){}
        explicit T_Vector(T_Vector<T>& other)
        {
                m_capicity = other.m_capicity;
                m_size = other.m_size;
                Allocator(other.m_size);

                for (size_t i = 0; i < m_size; ++i)
                        m_vec[i] = other.m_vec[i];
        }
        explicit T_Vector(T data, size_t sum = 1)
        {
                //sum 大于0
                assert(sum > 0);

                Allocator(sum);
                for (size_t i = 0; i < sum; ++i)
                        m_vec[i] = T_Counter<T>(data);
        }
        //初始化列表
        explicit T_Vector(const std::initializer_list<T>& init)
        {
                if (init.size() == 0)
                {
                        T_Vector();
                        return;
                }

                Allocator(init.size());
                
                for (const auto& item : init)
                        Append(item);
        }
        //////////////////    移动构造    //////////////////
        explicit T_Vector(T_Vector<T>&& other)
        {
                m_capicity = other.m_capicity;
                m_size = other.m_size;
                m_vec = other.m_vec;

                other.m_capicity = 0;
                other.m_size = 0;
                other.m_vec = nullptr;
        }
        //////////////////    析构函数    //////////////////
        ~T_Vector()
        {
                if (m_vec)
                        delete[] m_vec;

                m_size = 0;
                m_capicity = 0;
        }
        //////////////////    重载运算符    //////////////////
        T_Vector<T>& operator=(T_Vector& other)
        {
                m_capicity = other.m_capicity;
                m_size = other.m_size;
                if (m_vec) delete[] m_vec;
                Allocator(m_capicity);

                for (size_t i = 0; i < m_size; ++i)
                        m_vec[i] = other[i];

                return *this;
        }
        const T_Counter<T>& operator[](size_t position) { assert(position < m_size); return m_vec[position]; }

        //////////////////    公开接口    //////////////////

        //是否为空
        bool IsEmpty() const { if (m_size == 0) return true; else return false; }
        //添加 sum 个数据,内容为 data
        size_t Append(T data, size_t sum = 1)
        {
                assert(sum != 0);

                if (m_size + 1 < m_capicity)
                {
                        m_vec[m_size] = T_Counter<T>(data);
                        m_size += 1;
                }
                else
                {
                        AddAllocator();
                        m_vec[m_size] = T_Counter<T>(data);
                        m_size += 1;
                }

                return m_size - 1;
        }
        //删除 pos 位置的数据
        bool Erase(size_t pos)
        {
                if (pos >= m_size) return false;

                for (size_t i = pos; i < m_size; ++i)
                {
                        T_Counter<T> temp = m_vec[i + 1];
                        m_vec[i + 1] = m_vec[i];
                        m_vec[i] = temp;
                }

                m_size -= 1;
                return true;
        }
        //获取存储的数据的总数
        size_t Size() const { return m_size; }
        //获得容量
        size_t Capicity() const { return m_capicity; }

private:
        //申请空间
        void Allocator(size_t size) { m_vec = new T_Counter<T>[size]; m_capicity = size; m_size = 0; }
        //重新申请空间
        void ReAllocator(size_t size)
        {
                if (m_vec == nullptr) { Allocator(size); }

                T_Counter<T>* temp = new T_Counter<T>[size];

                for (size_t i = 0; i < m_size; ++i)
                        temp[i] = m_vec[i];

                T_Counter<T>* t = m_vec;
                delete[] t;

                m_vec = temp;
                m_capicity = size;
        }
        //增加空间
        void AddAllocator()
        {
                if (m_vec == nullptr) Allocator(5);

                ReAllocator(m_capicity * 2);
        }

        //数据指针
        T_Counter<T>*        m_vec;
        //自身数据总数
        size_t                        m_size;
        //自身容量大小
        size_t                        m_capicity;
};
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-12 14:54:03 | 显示全部楼层    本楼为最佳答案   
本帖最后由 yuxijian2020 于 2021-5-12 15:02 编辑

工具人来了
#include <iostream>
#include <initializer_list>
#include <cassert>

template<class T>
class T_Counter
{
public:
        //////////////////    默认构造    //////////////////
        T_Counter() { m_data = new T(); }
        //////////////////    拷贝构造    //////////////////
        explicit T_Counter(T_Counter<T>& other)
        {
                m_data = new T();
                *m_data = *other.m_data;
        }
        explicit T_Counter(const T_Counter<T>& other)
        {
                m_data = new T();
                *m_data = *other.m_data;
        }
        //////////////////    移动构造    //////////////////
        explicit T_Counter(T_Counter<T>&& other)
        {
                m_data = other.m_data;
                other.m_data = nullptr;
        }
        explicit T_Counter(T&& data)
        {
                m_data = new T();
                *m_data = data;
        }
        //////////////////    单参数构造    //////////////////
        explicit T_Counter(T& data)
        {
                m_data = new T();
                *m_data = data;
        }
        //////////////////    析构    //////////////////
        ~T_Counter() { if (m_data) delete m_data; }

        //////////////////    重载运算符    //////////////////
        T_Counter<T>& operator=(const T_Counter<T>& other) { *m_data = *other.m_data; return *this; }
        T_Counter<T> operator+(T_Counter<T>& other) { return T_Counter<T>(*m_data + *other.m_data); }
        T_Counter<T> operator-(const T_Counter<T>& other) { return T_Counter<T>(*m_data - *other.m_data); }
        T_Counter<T>& operator+=(const T_Counter<T>& other) { *m_data += *other.m_data; return *this; }
        T_Counter<T>& operator-=(const T_Counter<T>& other) { *m_data -= *other.m_data; return *this; }

        //////////////////    重载友元流操作符    //////////////////
        friend std::ostream& operator<<(std::ostream& os, const T_Counter<T>& t) { os << *t.m_data; return os; }
        friend std::istream& operator>>(std::istream& is, const T_Counter<T>& t) { is >> *t.m_data; return is; }

        //////////////////    公开接口    //////////////////

        //设置数据内容
        void SetData(const T& data) { *m_data = data; }
        //获取数据内容
        T GetData() const { return *m_data; }

private:
        T* m_data;
};

template<class T>
class T_Vector
{
public:
        //////////////////    默认构造    //////////////////
        explicit T_Vector() : m_vec(nullptr), m_size(0), m_capicity(0){}
        explicit T_Vector(T_Vector<T>& other)
        {
                m_capicity = other.m_capicity;
                m_size = other.m_size;
                Allocator(other.m_size);

                for (size_t i = 0; i < m_size; ++i)
                        m_vec[i] = other.m_vec[i];
        }
        explicit T_Vector(T data, size_t sum = 1)
        {
                //sum 大于0
                assert(sum > 0);

                Allocator(sum);
                for (size_t i = 0; i < sum; ++i)
                        m_vec[i] = T_Counter<T>(data);
        }
        //初始化列表
        explicit T_Vector(const std::initializer_list<T>& init)
        {
                if (init.size() == 0)
                {
                        T_Vector();
                        return;
                }

                Allocator(init.size());
                
                for (const auto& item : init)
                        Append(item);
        }
        //////////////////    移动构造    //////////////////
        explicit T_Vector(T_Vector<T>&& other)
        {
                m_capicity = other.m_capicity;
                m_size = other.m_size;
                m_vec = other.m_vec;

                other.m_capicity = 0;
                other.m_size = 0;
                other.m_vec = nullptr;
        }
        //////////////////    析构函数    //////////////////
        ~T_Vector()
        {
                if (m_vec)
                        delete[] m_vec;

                m_size = 0;
                m_capicity = 0;
        }
        //////////////////    重载运算符    //////////////////
        T_Vector<T>& operator=(T_Vector& other)
        {
                m_capicity = other.m_capicity;
                m_size = other.m_size;
                if (m_vec) delete[] m_vec;
                Allocator(m_capicity);

                for (size_t i = 0; i < m_size; ++i)
                        m_vec[i] = other[i];

                return *this;
        }
        const T_Counter<T>& operator[](size_t position) { assert(position < m_size); return m_vec[position]; }

        //////////////////    公开接口    //////////////////

        //是否为空
        bool IsEmpty() const { if (m_size == 0) return true; else return false; }
        //添加 sum 个数据,内容为 data
        size_t Append(T data, size_t sum = 1)
        {
                assert(sum != 0);

                if (m_size + 1 < m_capicity)
                {
                        m_vec[m_size] = T_Counter<T>(data);
                        m_size += 1;
                }
                else
                {
                        AddAllocator();
                        m_vec[m_size] = T_Counter<T>(data);
                        m_size += 1;
                }

                return m_size - 1;
        }
        //删除 pos 位置的数据
        bool Erase(size_t pos)
        {
                if (pos >= m_size) return false;

                for (size_t i = pos; i < m_size; ++i)
                {
                        T_Counter<T> temp = m_vec[i + 1];
                        m_vec[i + 1] = m_vec[i];
                        m_vec[i] = temp;
                }

                m_size -= 1;
                return true;
        }
        //获取存储的数据的总数
        size_t Size() const { return m_size; }
        //获得容量
        size_t Capicity() const { return m_capicity; }

private:
        //申请空间
        void Allocator(size_t size) { m_vec = new T_Counter<T>[size]; m_capicity = size; m_size = 0; }
        //重新申请空间
        void ReAllocator(size_t size)
        {
                if (m_vec == nullptr) { Allocator(size); }

                T_Counter<T>* temp = new T_Counter<T>[size];

                for (size_t i = 0; i < m_size; ++i)
                        temp[i] = m_vec[i];

                T_Counter<T>* t = m_vec;
                delete[] t;

                m_vec = temp;
                m_capicity = size;
        }
        //增加空间
        void AddAllocator()
        {
                if (m_vec == nullptr) Allocator(5);

                ReAllocator(m_capicity * 2);
        }

        //数据指针
        T_Counter<T>*        m_vec;
        //自身数据总数
        size_t                        m_size;
        //自身容量大小
        size_t                        m_capicity;
};
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-12 17:18:38 | 显示全部楼层
突然发现有个地方写错了
//初始化列表
        explicit T_Vector(const std::initializer_list<T>& init)
        {
                if (init.size() == 0)
                {
                        T_Vector();
                        return;
                }

                Allocator(init.size());
               
                for (const auto& item : init)
                        Append(item);
        }
改成
//初始化列表
        explicit T_Vector(const std::initializer_list<T>& init) : m_vec(nullptr), m_size(0), m_capicity(0)
        {
                if (init.size() == 0) return;

                Allocator(init.size());
                
                for (const auto& item : init)
                        Append(item);
        }
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 08:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表