• 简单的vector--- 2


    如何重载operator[]   及其相关细节

    如何使用 const_cast<>(  )  和 static_cast<>( )

    模板类 如何内部声明,外部定义友元函数

    使用memset( )、memcpy_s( )

    使用sizeof( )

    禁用移动构造 和 移动赋值

      1 #pragma once
      2 #include <iostream>
      3 using std::ostream;
      4 using std::cout;
      5 using std::endl;
      6 
      7 template <class T>
      8 class vector
      9 {
     10 public:
     11     explicit vector(int size);
     12     vector(const vector<T>& v);
     13     vector(vector<T>&&) = delete;
     14     vector(int size, T vlaue);
     15     vector(T* es, int len);
     16     vector& operator=(const vector<T>& v);
     17     vector& operator=(vector<T>&&) = delete;
     18 
     19     int size() const;
     20     int capacity() const;
     21     void reserve(int min_capacity);
     22     void push_back(T e);
     23     void pop_back();
     24     void erase(int pos);
     25     void insert(T e, int pos);
     26 
     27     const T& operator[](int) const;
     28     T& operator[](int);
     29     template <class Ty>
     30     friend ostream& operator<<(ostream& os, const vector<Ty>& v);
     31 private:
     32     T* vec;
     33     int _size;
     34     int _capacity;
     35 };
     36 
     37 template <class T>
     38 inline ostream& operator<<(ostream& os, const vector<T>& v)
     39 {
     40     os << endl << "输出: " << endl;
     41     for (int i = 0; i < v._size; i++)
     42     {
     43         os << v.vec[i] << "  ";
     44     }
     45     return os;
     46 }
     47 
     48 template <class T>
     49 inline vector<T>::vector(int size) : _size(size)
     50 {
     51     _capacity = size + size / 3;
     52     this->vec = new T[_capacity];
     53     memset(this->vec, 0x00, sizeof(T) * _capacity);
     54 }
     55 
     56 template <class T>
     57 inline vector<T>::vector(int size, T vlaue)
     58 {
     59     this->_capacity = size + size / 3;
     60     this->_size = size;
     61     this->vec = new T[_capacity];
     62     for (int i = 0; i < _size; i++)
     63     {
     64         this->vec[i] = vlaue;
     65     }
     66 }
     67 
     68 template <class T>
     69 inline vector<T>::vector(T* es, int len)
     70 {
     71     this->_size = len;
     72     this->_capacity = len + len / 3;
     73     this->vec = new T[_capacity];
     74     memcpy_s(this->vec, len * sizeof(T), es, len * sizeof(T));
     75 }
     76 
     77 template <class T>
     78 inline vector<T>::vector(const vector& v)
     79 {
     80     this->_capacity = v._capacity;
     81     this->_size = v._size;
     82     this->vec = new T[_capacity];
     83     memcpy_s(this->vec, sizeof(T) * _capacity, v.vec, sizeof(T) * _capacity);
     84 }
     85 
     86 template <class T>
     87 inline int vector<T>::size() const
     88 {
     89     return this->_size;
     90 }
     91 
     92 template <class T>
     93 inline int vector<T>::capacity() const
     94 {
     95     return this->_capacity;
     96 }
     97 
     98 template <class T>
     99 inline vector<T>& vector<T>::operator=(const vector<T>& v)
    100 {
    101     if (this == &v)return *this;
    102     if (this->vec != nullptr)delete[] this->vec;
    103     this->_capacity = v._capacity;
    104     this->_size = v._size;
    105     this->vec = new T[_capacity];
    106     memcpy_s(this->vec, _capacity * sizeof(T), v.vec, v._capacity * sizeof(T));
    107     return *this;
    108 }
    109 
    110 template <class T>
    111 inline void vector<T>::reserve(const int min_capacity)
    112 {
    113     if (min_capacity < this->_capacity) return;
    114     else
    115     {
    116         int* n_vec = new T[min_capacity];
    117         for (int i = 0; i < _size; i++)
    118         {
    119             n_vec[i] = vec[i];
    120         }
    121         delete[] vec;
    122         this->vec = n_vec;
    123         this->_capacity = min_capacity;
    124     }
    125 }
    126 
    127 template <class T>
    128 inline void vector<T>::push_back(T e)
    129 {
    130     if (this->_size >= this->_capacity)
    131     {
    132         this->reserve(this->_size + this->_size / 3);
    133     }
    134     this->vec[_size++] = e;
    135 }
    136 
    137 template <class T>
    138 inline void vector<T>::pop_back()
    139 {
    140     if (this->_size != 0)
    141         this->_size -= 1;
    142 }
    143 
    144 template <class T>
    145 inline void vector<T>::erase(int pos)
    146 {
    147     if (pos < 0 || pos >= _size)return;
    148     else
    149     {
    150         for (int i = pos; i < _size - 1; i++)
    151         {
    152             this->vec[i] = this->vec[i + 1];
    153         }
    154         _size -= 1;
    155     }
    156 }
    157 
    158 template <class T>
    159 inline void vector<T>::insert(T e, int pos)
    160 {
    161     if (pos < 0 || pos > _size - 1)return;
    162     if (this->_size >= this->_capacity)
    163     {
    164         this->reserve(this->_size + this->_size / 3);
    165     }
    166     for (int i = _size; i > pos; i++)
    167     {
    168         this->vec[i] = this->vec[i - 1];
    169     }
    170     this->vec[pos] = e;
    171 }
    172 
    173 template <class T>
    174 inline const T& vector<T>::operator[](int i) const
    175 {
    176     if (i < 0 || i >= this->_size)
    177     {
    178         cout << "下标 i=" << i << " 越界 退出";
    179         exit(1);
    180     };
    181     return this->vec[i];
    182 }
    183 
    184 template <class T>
    185 T& vector<T>::operator[](int pos)
    186 {
    187     // 这个写法的意思是 将指针this 转成 const 型指针 再对其*解引 
    188     //return const_cast<T&>((*static_cast<const vector<T>*>(this))[pos]);
    189 
    190     //这个是先对this 指针解引  再转成const 而且&直接引用该对象 
    191     return const_cast<T&>(static_cast<const vector<T>&>(*this)[pos]);
    192 
    193     //这个写法不不可取;先对this指针 解引,再转成const时 并不是&,所以会重新创建一个新对象
    194     //return const_cast<T&>(static_cast<const vector<T>>(*this)[pos]);
    195 }
  • 相关阅读:
    SEO网站优化10大要点
    三维翻动效果的jquery特效代码
    多款国外虚拟主机简单比较
    jquery同步调用ajax
    3D虚拟技术
    最简单jquery.ajax+php例子(对话框显示文本框输入内容),以小见大(初学手记)
    正则表达式学习博客
    关于XHTML头部声明,什么是DOCTYPE?
    Iframe高度自适应(兼容IE/Firefox、同域/跨域)
    3D立体产业链的发展现状和趋势
  • 原文地址:https://www.cnblogs.com/infoo/p/7691902.html
Copyright © 2020-2023  润新知