1. 程式人生 > >143.vector模板庫

143.vector模板庫

change 數組 eal sizeof hang names ont bsp chan

  • myvector.h
     1 #pragma once
     2 #include <initializer_list>
     3 #include <iostream>
     4 using namespace std;
     5 
     6 template<class T>
     7 class myvector
     8 {
     9 public:
    10     myvector();
    11     myvector(int n);
    12     myvector(initializer_list<T> my);
    13     void show() const;
    14
    ~myvector(); 15 //用於叠代 16 T *begin(); 17 T *end(); 18 int arraysize(); 19 int memsize(); 20 //後插 21 void push_back(T data); 22 //前插 23 void push_front(T data); 24 //返回的是副本 25 T operator[](int i) const; 26 //返回的不是副本 27 T &operator[](int i); 28 //發現左值
    29 T* find(T &t); 30 //發現右值 31 T* find(T &&t); 32 //改變一個值,傳遞的是右值 33 void change(T *pos, T &&t); 34 //改變一個值,傳遞的是左值 35 void change(T *pos, T &t); 36 void del(T &t); 37 void del(T &&t); 38 //找到的位置之前插入 39 void insert(T &t, T &newt);
    40 void insert(T &&t, T &&newt); 41 42 template<class T> 43 friend ostream& operator <<(ostream &out, myvector<T> &myv); 44 public: 45 T * p; 46 int memn;//內存長度 47 int realn;//數組長度 48 };

  • myvector.cpp
      1 #include "myvector.h"
      2 
      3 template<class T>
      4 myvector<T>::myvector():p(nullptr),memn(0),realn(0)
      5 {
      6     
      7 }
      8 
      9 template<class T>
     10 myvector<T>::myvector(int n)
     11 {
     12     this->memn = this->realn = n;
     13     this->p = new T[n];
     14     memset(this->p, 0, sizeof(T)*n);
     15 }
     16 
     17 template<class T>
     18 myvector<T>::myvector(initializer_list<T> my)
     19 {
     20     this->realn = this->memn = my.size();
     21     this->p = new T[my.size()];
     22     int id = 0;
     23     //初始化
     24     for (auto i : my)
     25     {
     26         this->p[id] = i;
     27         id++;
     28     }
     29 }
     30 
     31 template<class T>
     32 void myvector<T>::show() const
     33 {
     34     for (int i = 0; i < realn; i++)
     35     {
     36         cout << p[i];
     37     }
     38 }
     39 
     40 template<class T>
     41 myvector<T>::~myvector()
     42 {
     43     if (p != nullptr)
     44     {
     45         delete[] this->p;
     46     }
     47 }
     48 
     49 template<class T>
     50 T * myvector<T>::begin()
     51 {
     52     return this->p;
     53 }
     54 
     55 template<class T>
     56 T * myvector<T>::end()
     57 {
     58     return this->p + this->realn;
     59 }
     60 
     61 template<class T>
     62 int myvector<T>::arraysize()
     63 {
     64     return this->realn;
     65 }
     66 
     67 template<class T>
     68 int myvector<T>::memsize()
     69 {
     70     return this->memn;
     71 }
     72 
     73 template<class T>
     74 void myvector<T>::push_back(T data)
     75 {
     76     if (this->p == nullptr || this->memn == 0)
     77     {
     78             p = new T;
     79             *p = data;
     80             memn = 1;
     81             realn = 1;
     82     }
     83     else if(memn == realn)
     84     {
     85         T *ptemp = new T[this->memn + 1];
     86         memcpy(ptemp, this->p, this->realn * sizeof(T));
     87         *(ptemp + realn) = data;
     88         delete[] this->p;
     89         this->p = ptemp;
     90         realn += 1;
     91         memn += 1;
     92     }
     93     else
     94     {
     95         p[realn] = data;
     96         realn++;
     97     }
     98 }
     99 
    100 template<class T>
    101 void myvector<T>::push_front(T  data)
    102 {
    103     if (this->p == nullptr || this->memn == 0)
    104     {
    105         p = new T;
    106         *p = data;
    107         memn = 1;
    108         realn = 1;
    109     }
    110     else if (memn == realn)
    111     {
    112         T *ptemp = new T[this->memn + 1];
    113         memcpy(ptemp + 1, this->p, this->realn * sizeof(T));
    114         *ptemp = data;
    115         delete[] this->p;
    116         this->p = ptemp;
    117         realn += 1;
    118         memn += 1;
    119     }
    120     else
    121     {
    122         for (int i = 0; i < realn; i++)
    123         {
    124             p[i + 1] = p[i];
    125         }
    126         p[0] = data;
    127         realn += 1;
    128     }
    129 }
    130 
    131 template<class T>
    132 T myvector<T>::operator[](int i) const
    133 {
    134     if (i > realn)
    135     {
    136         throw 1;
    137     }
    138     return this->p[i];
    139 }
    140 
    141 template<class T>
    142 T & myvector<T>::operator[](int i)
    143 {
    144     if (i > realn)
    145     {
    146         throw 2;
    147     }
    148     return this->p[i];
    149 }
    150 
    151 template<class T>
    152 T * myvector<T>::find(T & t)
    153 {
    154     for (auto ib = this->begin(); ib != this->end(); ib++)
    155     {
    156         if (*it == t)
    157         {
    158             return ib;
    159         }
    160     }
    161 }
    162 
    163 template<class T>
    164 T * myvector<T>::find(T && t)
    165 {
    166     for (auto ib = this->begin(); ib != this->end(); ib++)
    167     {
    168         if (*it == t)
    169         {
    170             return ib;
    171         }
    172     }
    173 }
    174 
    175 template<class T>
    176 void myvector<T>::change(T * pos, T && t)
    177 {
    178     if (pos != nullptr)
    179     {
    180         *pos = t;
    181     }
    182 }
    183 
    184 template<class T>
    185 void myvector<T>::change(T * pos, T & t)
    186 {
    187     if (pos != nullptr)
    188     {
    189         *pos = t;
    190     }
    191 }
    192 
    193 template<class T>
    194 void myvector<T>::del(T & t)
    195 {
    196     int pos = -1;
    197     for (int i = 0; i < this->realn; i++)
    198     {
    199         if (t == *(this->p + i))
    200         {
    201             pos = i;
    202             break;
    203         }
    204     }
    205     if (pos != -1)
    206     {
    207         if (pos == this->realn)
    208         {
    209             this->realn -= 1;
    210         }
    211         else
    212         {
    213             for (int i = pos; i < realn-1; i++)
    214             {
    215                 p[i] = p[i + 1];
    216             }
    217             realn -= 1;
    218         }
    219     }
    220 }
    221 
    222 template<class T>
    223 void myvector<T>::del(T && t)
    224 {
    225     int pos = -1;
    226     for (int i = 0; i < this->realn; i++)
    227     {
    228         if (t == *(this->p + i))
    229         {
    230             pos = i;
    231             break;
    232         }
    233     }
    234     if (pos != -1)
    235     {
    236         if (pos == this->realn)
    237         {
    238             this->realn -= 1;
    239         }
    240         else
    241         {
    242             for (int i = pos; i < realn - 1; i++)
    243             {
    244                 p[i] = p[i + 1];
    245             }
    246             realn -= 1;
    247         }
    248     }
    249 }
    250 
    251 
    252 template<class T>
    253 void myvector<T>::insert(T & t, T & newt)
    254 {
    255     int pos = -1;
    256     for (int i = 0; i < this->realn; i++)
    257     {
    258         if (t == *(this->p + i))
    259         {
    260             pos = i;
    261             break;
    262         }
    263     }
    264 
    265     if (pos != -1)
    266     {
    267         if (this->realn == this->memn)
    268         {
    269                 T *ptemp = new T[this->memn + 1];
    270                 memcpy(ptemp, this->p, sizeof(T)*memn);
    271 
    272                 delete[] this->p;
    273                 this->p = ptemp;
    274                 this->realn += 1;
    275                 this->memn += 1;
    276                 for (int i = realn - 2; i >= pos; i--)
    277                 {
    278                     p[i + 1] = p[i];
    279                 }
    280                 p[pos] = newt;
    281         }
    282         else
    283         {
    284             for (int i = realn - 2; i >= pos; i--)
    285             {
    286                 p[i + 1] = p[i];
    287             }
    288             p[pos] = newt;
    289             this->realn += 1;
    290         }
    291     }
    292     
    293 }
    294 
    295 template<class T>
    296 void myvector<T>::insert(T && t, T && newt)
    297 {
    298     int pos = -1;
    299     for (int i = 0; i < this->realn; i++)
    300     {
    301         if (t == *(this->p + i))
    302         {
    303             pos = i;
    304             break;
    305         }
    306     }
    307 
    308     if (pos != -1)
    309     {
    310         if (this->realn == this->memn)
    311         {
    312             T *ptemp = new T[this->memn + 1];
    313             memcpy(ptemp, this->p, sizeof(T)*memn);
    314 
    315             delete[] this->p;
    316             this->p = ptemp;
    317             this->realn += 1;
    318             this->memn += 1;
    319             for (int i = realn - 2; i >= pos; i--)
    320             {
    321                 p[i + 1] = p[i];
    322             }
    323             p[pos] = newt;
    324         }
    325         else
    326         {
    327             for (int i = realn - 2; i >= pos; i--)
    328             {
    329                 p[i + 1] = p[i];
    330             }
    331             p[pos] = newt;
    332             this->realn += 1;
    333         }
    334     }
    335 }
    336 
    337 template<class T>
    338 ostream & operator<<(ostream & out, myvector<T> & myv)
    339 {
    340     for (int i = 0; i < myv.realn; i++)
    341     {
    342         out << myv.p[i];
    343     }
    344     return out;
    345 }

  • main.cpp
     1 #include "myvector.h"
     2 #include "myvector.cpp"
     3 
     4 void main()
     5 {
     6     myvector<double> myv({ 1.1,2.2,3.3 });
     7     //myv.show();
     8     ////叠代器
     9     //for (auto i : myv)
    10     //{
    11     //    cout << i << endl;
    12     //}
    13     myv.push_front(4.4);
    14     myv.push_back(5.5);
    15     myv.insert(2.2, 8.8);
    16     myv.del(2.2);
    17     myv.push_back(2.2);
    18     myv.del(2.2);
    19     /*for (auto ib = myv.begin(), ie = myv.end(); ib != ie; ib++)
    20     {
    21         cout << *ib << endl;
    22     }*/
    23     //cout << myv[1] << endl;
    24     cout << myv << endl;
    25     myv.show();
    26     cin.get();
    27 }

143.vector模板庫