鱼C论坛

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

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

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

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

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

x

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

工具人来了

  1. #include <iostream>
  2. #include <initializer_list>
  3. #include <cassert>

  4. template<class T>
  5. class T_Counter
  6. {
  7. public:
  8.         //////////////////    默认构造    //////////////////
  9.         T_Counter() { m_data = new T(); }
  10.         //////////////////    拷贝构造    //////////////////
  11.         explicit T_Counter(T_Counter<T>& other)
  12.         {
  13.                 m_data = new T();
  14.                 *m_data = *other.m_data;
  15.         }
  16.         explicit T_Counter(const T_Counter<T>& other)
  17.         {
  18.                 m_data = new T();
  19.                 *m_data = *other.m_data;
  20.         }
  21.         //////////////////    移动构造    //////////////////
  22.         explicit T_Counter(T_Counter<T>&& other)
  23.         {
  24.                 m_data = other.m_data;
  25.                 other.m_data = nullptr;
  26.         }
  27.         explicit T_Counter(T&& data)
  28.         {
  29.                 m_data = new T();
  30.                 *m_data = data;
  31.         }
  32.         //////////////////    单参数构造    //////////////////
  33.         explicit T_Counter(T& data)
  34.         {
  35.                 m_data = new T();
  36.                 *m_data = data;
  37.         }
  38.         //////////////////    析构    //////////////////
  39.         ~T_Counter() { if (m_data) delete m_data; }

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

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

  49.         //////////////////    公开接口    //////////////////

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

  54. private:
  55.         T* m_data;
  56. };

  57. template<class T>
  58. class T_Vector
  59. {
  60. public:
  61.         //////////////////    默认构造    //////////////////
  62.         explicit T_Vector() : m_vec(nullptr), m_size(0), m_capicity(0){}
  63.         explicit T_Vector(T_Vector<T>& other)
  64.         {
  65.                 m_capicity = other.m_capicity;
  66.                 m_size = other.m_size;
  67.                 Allocator(other.m_size);

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

  75.                 Allocator(sum);
  76.                 for (size_t i = 0; i < sum; ++i)
  77.                         m_vec[i] = T_Counter<T>(data);
  78.         }
  79.         //初始化列表
  80.         explicit T_Vector(const std::initializer_list<T>& init)
  81.         {
  82.                 if (init.size() == 0)
  83.                 {
  84.                         T_Vector();
  85.                         return;
  86.                 }

  87.                 Allocator(init.size());
  88.                
  89.                 for (const auto& item : init)
  90.                         Append(item);
  91.         }
  92.         //////////////////    移动构造    //////////////////
  93.         explicit T_Vector(T_Vector<T>&& other)
  94.         {
  95.                 m_capicity = other.m_capicity;
  96.                 m_size = other.m_size;
  97.                 m_vec = other.m_vec;

  98.                 other.m_capicity = 0;
  99.                 other.m_size = 0;
  100.                 other.m_vec = nullptr;
  101.         }
  102.         //////////////////    析构函数    //////////////////
  103.         ~T_Vector()
  104.         {
  105.                 if (m_vec)
  106.                         delete[] m_vec;

  107.                 m_size = 0;
  108.                 m_capicity = 0;
  109.         }
  110.         //////////////////    重载运算符    //////////////////
  111.         T_Vector<T>& operator=(T_Vector& other)
  112.         {
  113.                 m_capicity = other.m_capicity;
  114.                 m_size = other.m_size;
  115.                 if (m_vec) delete[] m_vec;
  116.                 Allocator(m_capicity);

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

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

  122.         //////////////////    公开接口    //////////////////

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

  129.                 if (m_size + 1 < m_capicity)
  130.                 {
  131.                         m_vec[m_size] = T_Counter<T>(data);
  132.                         m_size += 1;
  133.                 }
  134.                 else
  135.                 {
  136.                         AddAllocator();
  137.                         m_vec[m_size] = T_Counter<T>(data);
  138.                         m_size += 1;
  139.                 }

  140.                 return m_size - 1;
  141.         }
  142.         //删除 pos 位置的数据
  143.         bool Erase(size_t pos)
  144.         {
  145.                 if (pos >= m_size) return false;

  146.                 for (size_t i = pos; i < m_size; ++i)
  147.                 {
  148.                         T_Counter<T> temp = m_vec[i + 1];
  149.                         m_vec[i + 1] = m_vec[i];
  150.                         m_vec[i] = temp;
  151.                 }

  152.                 m_size -= 1;
  153.                 return true;
  154.         }
  155.         //获取存储的数据的总数
  156.         size_t Size() const { return m_size; }
  157.         //获得容量
  158.         size_t Capicity() const { return m_capicity; }

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

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

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

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

  171.                 m_vec = temp;
  172.                 m_capicity = size;
  173.         }
  174.         //增加空间
  175.         void AddAllocator()
  176.         {
  177.                 if (m_vec == nullptr) Allocator(5);

  178.                 ReAllocator(m_capicity * 2);
  179.         }

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

使用道具 举报

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

工具人来了

  1. #include <iostream>
  2. #include <initializer_list>
  3. #include <cassert>

  4. template<class T>
  5. class T_Counter
  6. {
  7. public:
  8.         //////////////////    默认构造    //////////////////
  9.         T_Counter() { m_data = new T(); }
  10.         //////////////////    拷贝构造    //////////////////
  11.         explicit T_Counter(T_Counter<T>& other)
  12.         {
  13.                 m_data = new T();
  14.                 *m_data = *other.m_data;
  15.         }
  16.         explicit T_Counter(const T_Counter<T>& other)
  17.         {
  18.                 m_data = new T();
  19.                 *m_data = *other.m_data;
  20.         }
  21.         //////////////////    移动构造    //////////////////
  22.         explicit T_Counter(T_Counter<T>&& other)
  23.         {
  24.                 m_data = other.m_data;
  25.                 other.m_data = nullptr;
  26.         }
  27.         explicit T_Counter(T&& data)
  28.         {
  29.                 m_data = new T();
  30.                 *m_data = data;
  31.         }
  32.         //////////////////    单参数构造    //////////////////
  33.         explicit T_Counter(T& data)
  34.         {
  35.                 m_data = new T();
  36.                 *m_data = data;
  37.         }
  38.         //////////////////    析构    //////////////////
  39.         ~T_Counter() { if (m_data) delete m_data; }

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

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

  49.         //////////////////    公开接口    //////////////////

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

  54. private:
  55.         T* m_data;
  56. };

  57. template<class T>
  58. class T_Vector
  59. {
  60. public:
  61.         //////////////////    默认构造    //////////////////
  62.         explicit T_Vector() : m_vec(nullptr), m_size(0), m_capicity(0){}
  63.         explicit T_Vector(T_Vector<T>& other)
  64.         {
  65.                 m_capicity = other.m_capicity;
  66.                 m_size = other.m_size;
  67.                 Allocator(other.m_size);

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

  75.                 Allocator(sum);
  76.                 for (size_t i = 0; i < sum; ++i)
  77.                         m_vec[i] = T_Counter<T>(data);
  78.         }
  79.         //初始化列表
  80.         explicit T_Vector(const std::initializer_list<T>& init)
  81.         {
  82.                 if (init.size() == 0)
  83.                 {
  84.                         T_Vector();
  85.                         return;
  86.                 }

  87.                 Allocator(init.size());
  88.                
  89.                 for (const auto& item : init)
  90.                         Append(item);
  91.         }
  92.         //////////////////    移动构造    //////////////////
  93.         explicit T_Vector(T_Vector<T>&& other)
  94.         {
  95.                 m_capicity = other.m_capicity;
  96.                 m_size = other.m_size;
  97.                 m_vec = other.m_vec;

  98.                 other.m_capicity = 0;
  99.                 other.m_size = 0;
  100.                 other.m_vec = nullptr;
  101.         }
  102.         //////////////////    析构函数    //////////////////
  103.         ~T_Vector()
  104.         {
  105.                 if (m_vec)
  106.                         delete[] m_vec;

  107.                 m_size = 0;
  108.                 m_capicity = 0;
  109.         }
  110.         //////////////////    重载运算符    //////////////////
  111.         T_Vector<T>& operator=(T_Vector& other)
  112.         {
  113.                 m_capicity = other.m_capicity;
  114.                 m_size = other.m_size;
  115.                 if (m_vec) delete[] m_vec;
  116.                 Allocator(m_capicity);

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

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

  122.         //////////////////    公开接口    //////////////////

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

  129.                 if (m_size + 1 < m_capicity)
  130.                 {
  131.                         m_vec[m_size] = T_Counter<T>(data);
  132.                         m_size += 1;
  133.                 }
  134.                 else
  135.                 {
  136.                         AddAllocator();
  137.                         m_vec[m_size] = T_Counter<T>(data);
  138.                         m_size += 1;
  139.                 }

  140.                 return m_size - 1;
  141.         }
  142.         //删除 pos 位置的数据
  143.         bool Erase(size_t pos)
  144.         {
  145.                 if (pos >= m_size) return false;

  146.                 for (size_t i = pos; i < m_size; ++i)
  147.                 {
  148.                         T_Counter<T> temp = m_vec[i + 1];
  149.                         m_vec[i + 1] = m_vec[i];
  150.                         m_vec[i] = temp;
  151.                 }

  152.                 m_size -= 1;
  153.                 return true;
  154.         }
  155.         //获取存储的数据的总数
  156.         size_t Size() const { return m_size; }
  157.         //获得容量
  158.         size_t Capicity() const { return m_capicity; }

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

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

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

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

  171.                 m_vec = temp;
  172.                 m_capicity = size;
  173.         }
  174.         //增加空间
  175.         void AddAllocator()
  176.         {
  177.                 if (m_vec == nullptr) Allocator(5);

  178.                 ReAllocator(m_capicity * 2);
  179.         }

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

使用道具 举报

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

  9.                 Allocator(init.size());
  10.                
  11.                 for (const auto& item : init)
  12.                         Append(item);
  13.         }
复制代码

改成
  1. //初始化列表
  2.         explicit T_Vector(const std::initializer_list<T>& init) : m_vec(nullptr), m_size(0), m_capicity(0)
  3.         {
  4.                 if (init.size() == 0) return;

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 20:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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