• C/C++ 的使用


    C++    http://www.cplusplus.com/

    http://www.cplusplus.me/

    *****************容器container vector

    转自 http://blog.csdn.net/qscool1987/article/details/7050487

    [cpp] view plaincopy           
     
    1. #ifndef _MY_VECTOR_H  
    2. #define _MY_VECTOR_H  
    3. #include <string.h>  
    4. #include <assert.h>  
    5. template<class T>  
    6. class MyVector  
    7. {  
    8.     public:  
    9.     class iterator  
    10.     {  
    11.         public:  
    12.         iterator():pelem(NULL){}  
    13.         iterator(T *pt):pelem(pt){}  
    14.         iterator(const iterator &iter);  
    15.         iterator& operator = (const iterator &iter);  
    16.         iterator& operator = (T *pt);  
    17.         ~iterator(){}  
    18.         bool operator != (const iterator &iter);  
    19.         iterator& operator ++ ();  
    20.         iterator& operator ++ (int);  
    21.         iterator& operator -- ();  
    22.         iterator& operator -- (int);  
    23.         iterator  operator + (size_t size);  
    24.         iterator  operator - (size_t size);  
    25.         iterator& operator -= (size_t size);  
    26.         iterator& operator += (size_t size);  
    27.         T& operator * ();  
    28.   
    29.         //functions add here  
    30.         private:  
    31.         T *pelem;  
    32.     };  
    33.     //constructor  
    34.     MyVector():pbuff(NULL),beg(NULL),last(NULL),count(0),capcity(0){}  
    35.     MyVector(const MyVector &orig);  
    36.     MyVector& operator = (const MyVector &orig);  
    37.     ~MyVector();  
    38.     //member function  
    39.     T& operator [] (size_t index);  
    40.     void pushback(const T &mt);  
    41.     iterator insert(size_t index,const T &mt);  
    42.     iterator insert(const T *phead,const T *pback, iterator p);  
    43.     iterator erase(size_t index);  
    44.     iterator erase(iterator phead, iterator pback);  
    45.     void clear();  
    46.     size_t size();  
    47.     size_t capacity();  
    48.     iterator begin();  
    49.     iterator end();  
    50.   
    51.     private:  
    52.     void del_buff()  
    53.     {  
    54.         if(NULL != pbuff)  
    55.         {  
    56.             delete pbuff;  
    57.             pbuff = NULL;  
    58.         }  
    59.     }  
    60.     T *pbuff;//Memory buff for elements  
    61.     iterator beg;  
    62.     iterator last;  
    63.     size_t count;  
    64.     size_t capcity;  
    65. };  
    66. /**MyVector's member functions**/  
    67. /**here are the member functions implementations**/  
    68. template<class T>  
    69. size_t MyVector<T>::size()  
    70. {  
    71.     return count;  
    72. }  
    73. template<class T>  
    74. size_t MyVector<T>::capacity()  
    75. {  
    76.     return capcity;  
    77. }  
    78. template<class T>  
    79. MyVector<T>::MyVector(const MyVector<T> &orig)  
    80. {  
    81.     count = orig.size();  
    82.     capcity = 2*count;  
    83.     pbuff = new T [count*2];  
    84.     size_t totalbytes = count*2*sizeof(T);  
    85.     memcpy(pbuff,orig.pbuff,totalbytes);  
    86. }  
    87. template<class T>  
    88. MyVector<T>& MyVector<T>::operator = (const MyVector<T> &orig)  
    89. {  
    90.     del_buff();  
    91.     count = orig.size();  
    92.     capcity = 2*count;  
    93.     pbuff = new T [count*2];  
    94.     size_t totalbytes = count*2*sizeof(T);  
    95.     memcpy(pbuff,orig.pbuff,totalbytes);  
    96.     return *this;  
    97. }  
    98. template<class T>  
    99. MyVector<T>::~MyVector<T>()  
    100. {  
    101.     del_buff();  
    102. }  
    103. template<class T>  
    104. T& MyVector<T>::operator[](size_t index)  
    105. {  
    106.     return pbuff[index];  
    107. }  
    108. template<class T>  
    109. void MyVector<T>::pushback(const T &mt)  
    110. {  
    111.     if(NULL == pbuff && 0 == count)  
    112.     {  
    113.         pbuff = new T[(1+count)*2];  
    114.         pbuff[0] = mt;  
    115.         count++;  
    116.         capcity = 2*count;  
    117.     }  
    118.     else  
    119.     {  
    120.         if(NULL != pbuff && count == capcity)  
    121.         {  
    122.             capcity *= 2;  
    123.             T *ptem = new T[capcity];  
    124.             size_t totalbytes = capcity*sizeof(T);  
    125.             memcpy(ptem,pbuff,totalbytes);  
    126.             del_buff();  
    127.             pbuff = ptem;  
    128.             pbuff[count] = mt;  
    129.             count ++;  
    130.         }  
    131.         if(NULL != pbuff && count != capcity)  
    132.         {  
    133.             pbuff[count] = mt;  
    134.             count++;  
    135.         }  
    136.     }  
    137. }  
    138. template<class T>  
    139. typename MyVector<T>::iterator MyVector<T>::insert(size_t index,const T &mt)  
    140. {  
    141.     assert(count >= index);  
    142.   
    143.     if(NULL != pbuff && count == capcity)  
    144.     {  
    145.         capcity *= 2;  
    146.         T *ptem = new T[capcity];  
    147.         memcpy(ptem,pbuff,capcity*sizeof(T));  
    148.         ptem[index] = mt;  
    149.         memcpy(&ptem[index+1],(count-index)*sizeof(T));  
    150.         del_buff();  
    151.         pbuff = ptem;  
    152.         count ++;  
    153.         typename MyVector<T>::iterator _iter(&pbuff[index]);  
    154.         return _iter;  
    155.     }  
    156.     else if(NULL != pbuff && count != capcity)  
    157.     {  
    158.         size_t _end = count-1;  
    159.         size_t _beg = index;  
    160.         for(;_end >= _beg;_end--)  
    161.             pbuff[_end+1] = pbuff[_end];  
    162.         pbuff[index] = mt;  
    163.         count++;  
    164.         typename MyVector<T>::iterator _iter(&pbuff[index]);  
    165.         return _iter;  
    166.     }  
    167. }  
    168. template<class T>  
    169. typename MyVector<T>::iterator MyVector<T>::  
    170.     insert(const T *phead,const T *pback,  
    171.              typename MyVector<T>::iterator p)  
    172. {  
    173.     typename MyVector<T>::iterator _beg = begin(),_end = end();  
    174.     size_t insertnum = 0;  
    175.     for(;phead != pback;phead++)  
    176.         insertnum++;  
    177.     phead -= insertnum;  
    178.     size_t index = 0;  
    179.     for(;_beg != p;_beg++)  
    180.         index++;  
    181.     if(count +insertnum > capcity && NULL != pbuff)  
    182.     {  
    183.         capcity = 2*(count +insertnum);  
    184.         T *ptem = new T [capcity];  
    185.         memcpy(ptem,pbuff,(index)*sizeof(T));  
    186.         memcpy(&ptem[index],phead,insertnum*sizeof(T));  
    187.         memcpy(&ptem[index+insertnum],&pbuff[index],  
    188.                 (count-index)*sizeof(T));  
    189.         del_buff();  
    190.         pbuff = ptem;  
    191.         count += insertnum;  
    192.         typename MyVector<T>::iterator _iter(&pbuff[index]);  
    193.         return _iter;  
    194.     }  
    195.     else if(count +insertnum <= capcity && NULL != pbuff)  
    196.     {  
    197.         for(size_t i = insertnum;i != 0;i--,count--)  
    198.             pbuff[count+insertnum-1] = pbuff[count-1];  
    199.         for(;phead != pback;phead++,p++)  
    200.             *p = *phead;  
    201.         count += insertnum;  
    202.   
    203.         return p;  
    204.     }  
    205.     if(NULL == pbuff && 0 == count)  
    206.     {  
    207.         capcity = 2*insertnum;  
    208.         pbuff = new T[capcity];  
    209.         memcpy(pbuff,phead,insertnum*sizeof(T));  
    210.         count = insertnum;  
    211.         typename MyVector<T>::iterator _iter(&pbuff[0]);  
    212.         return _iter;  
    213.     }  
    214. }  
    215. template<class T>  
    216. typename MyVector<T>::iterator MyVector<T>::erase(size_t index)  
    217. {  
    218.     T *temp = new T[count-index-1];  
    219.     memcpy(temp,&pbuff[index+1],(count-index-1)*sizeof(T));  
    220.     memcpy(&pbuff[index],temp,(count-index-1)*sizeof(T));  
    221.     pbuff[count-1] = '';  
    222.     count--;  
    223.     delete [] temp;  
    224.     typename MyVector<T>::iterator _iter(&pbuff[index]);  
    225.     return _iter;  
    226. }  
    227. template<class T>  
    228. typename MyVector<T>::iterator MyVector<T>::  
    229.     erase( typename MyVector<T>::iterator phead,  
    230.          typename MyVector<T>::iterator pback)  
    231. {  
    232.     size_t elemnum = 0;  
    233.     size_t _toend = 0;  
    234.     for(;phead != pback;phead++)  
    235.         elemnum++;  
    236.     phead -= elemnum;  
    237.     for(;pback != end();pback++)  
    238.         _toend++;  
    239.     pback -= _toend;  
    240.     T *temp = new T[_toend];  
    241.     memcpy(temp,&pbuff[count-_toend],_toend*sizeof(T));  
    242.     memcpy(&pbuff[count-elemnum-_toend],temp,_toend*sizeof(T));  
    243.     memset(&pbuff[count-elemnum],'',(count-elemnum)*sizeof(T));  
    244.     delete [] temp;  
    245.     count -= elemnum;  
    246.     return phead;  
    247. }  
    248. template<class T>  
    249. typename MyVector<T>::iterator MyVector<T>::begin()  
    250. {  
    251.     beg = &pbuff[0];  
    252.     return beg;  
    253. }  
    254. template<class T>  
    255. typename MyVector<T>::iterator MyVector<T>::end()  
    256. {  
    257.     last = &pbuff[count];  
    258.     return last;  
    259. }  
    260. /**nested dependent typeclass**/  
    261. /**implementation**/  
    262.   
    263. template<class T>  
    264. MyVector<T>::iterator::iterator(const typename MyVector<T>::iterator &iter)  
    265. {  
    266.     pelem = iter.pelem;  
    267. }  
    268. template<class T>  
    269. typename MyVector<T>::iterator& MyVector<T>::iterator::  
    270.     operator = (const typename MyVector<T>::iterator &iter)  
    271. {  
    272.     pelem = iter.pelem;  
    273.     return *this;  
    274. }  
    275. template<class T>  
    276. typename MyVector<T>::iterator& MyVector<T>::iterator::  
    277.     operator = (T *pt)  
    278. {  
    279.     pelem = pt;  
    280.     return *this;  
    281. }  
    282. template<class T>  
    283. bool MyVector<T>::iterator::operator !=  
    284.     (const typename MyVector<T>::iterator &iter)  
    285. {  
    286.     if(pelem != iter.pelem)  
    287.         return true;  
    288.     else  
    289.         return false;  
    290. }  
    291. template<class T>  
    292. typename MyVector<T>::iterator& MyVector<T>::iterator::operator ++ ()  
    293. {  
    294.     ++pelem;  
    295.     return *this;  
    296. }  
    297. template<class T>  
    298. typename MyVector<T>::iterator& MyVector<T>::iterator::operator ++ (int)  
    299. {  
    300.     pelem++;  
    301.     return *this;  
    302. }  
    303. template<class T>  
    304. typename MyVector<T>::iterator& MyVector<T>::iterator::operator -- (int)  
    305. {  
    306.     pelem--;  
    307.     return *this;  
    308. }  
    309. template<class T>  
    310. typename MyVector<T>::iterator& MyVector<T>::iterator::operator -- ()  
    311. {  
    312.     --pelem;  
    313.     return *this;  
    314. }  
    315. template<class T>  
    316. typename MyVector<T>::iterator& MyVector<T>::iterator::operator += (size_t size)  
    317. {  
    318.     pelem += size;  
    319.     return *this;  
    320. }  
    321. template<class T>  
    322. typename MyVector<T>::iterator& MyVector<T>::iterator::operator -= (size_t size)  
    323. {  
    324.     pelem -= size;  
    325.     return *this;  
    326. }  
    327. template<class T>  
    328. typename MyVector<T>::iterator MyVector<T>::iterator::operator + (size_t size)  
    329. {  
    330.     pelem += size;  
    331.     typename MyVector<T>::iterator _iter(pelem);  
    332.     return _iter;  
    333. }  
    334. template<class T>  
    335. typename MyVector<T>::iterator MyVector<T>::iterator::operator - (size_t size)  
    336. {  
    337.     pelem -= size;  
    338.     typename MyVector<T>::iterator _iter(pelem);  
    339.     return _iter;  
    340. }  
    341. template<class T>  
    342. T& MyVector<T>::iterator::operator * ()  
    343. {  
    344.     return *pelem;  
    345. }  
    346. #endif  
  • 相关阅读:
    阅读《Android 从入门到精通》(29)——四大布局
    UISegmentedControl 修改字体大小 和 颜色
    UITableView Scroll to top 手动设置tableview 滚动到 顶部
    xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cycle” in ARC-enabled code
    iOS key value coding kvc在接收json数据与 model封装中的使用
    NSData 转 bytes 字节数据
    git commit 出现 changes not staged for commit 错误
    iOS exit(),abort(),assert()函数区别
    ios 程序发布使用xcode工具Application Loader 正在通过ITUNES STORE进行鉴定错误
    iOS mac添加证书 不能修改“System Roots”钥匙串错误
  • 原文地址:https://www.cnblogs.com/hdu-edu/p/3953898.html
Copyright © 2020-2023  润新知