Vector部分实现

    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. //functions add here
    29. private:
    30. T *pelem;
    31. };
    32. //constructor
    33. MyVector():pbuff(NULL),beg(NULL),last(NULL),count(0),capcity(0){}
    34. MyVector(const MyVector &orig);
    35. MyVector& operator = (const MyVector &orig);
    36. ~MyVector();
    37. //member function
    38. T& operator [] (size_t index);
    39. void pushback(const T &mt);
    40. iterator insert(size_t index,const T &mt);
    41. iterator insert(const T *phead,const T *pback, iterator p);
    42. iterator erase(size_t index);
    43. iterator erase(iterator phead, iterator pback);
    44. void clear();
    45. size_t size();
    46. size_t capacity();
    47. iterator begin();
    48. iterator end();
    49. private:
    50. void del_buff()
    51. {
    52. if(NULL != pbuff)
    53. {
    54. delete pbuff;
    55. pbuff = NULL;
    56. }
    57. }
    58. T *pbuff;//Memory buff for elements
    59. iterator beg;
    60. iterator last;
    61. size_t count;
    62. size_t capcity;
    63. };
    64. /**MyVector‘s member functions**/
    65. /**here are the member functions implementations**/
    66. template<class T>
    67. size_t MyVector<T>::size()
    68. {
    69. return count;
    70. }
    71. template<class T>
    72. size_t MyVector<T>::capacity()
    73. {
    74. return capcity;
    75. }
    76. template<class T>
    77. MyVector<T>::MyVector(const MyVector<T> &orig)
    78. {
    79. count = orig.size();
    80. capcity = 2*count;
    81. pbuff = new T [count*2];
    82. size_t totalbytes = count*2*sizeof(T);
    83. memcpy(pbuff,orig.pbuff,totalbytes);
    84. }
    85. template<class T>
    86. MyVector<T>& MyVector<T>::operator = (const MyVector<T> &orig)
    87. {
    88. del_buff();
    89. count = orig.size();
    90. capcity = 2*count;
    91. pbuff = new T [count*2];
    92. size_t totalbytes = count*2*sizeof(T);
    93. memcpy(pbuff,orig.pbuff,totalbytes);
    94. return *this;
    95. }
    96. template<class T>
    97. MyVector<T>::~MyVector<T>()
    98. {
    99. del_buff();
    100. }
    101. template<class T>
    102. T& MyVector<T>::operator[](size_t index)
    103. {
    104. return pbuff[index];
    105. }
    106. template<class T>
    107. void MyVector<T>::pushback(const T &mt)
    108. {
    109. if(NULL == pbuff && 0 == count)
    110. {
    111. pbuff = new T[(1+count)*2];
    112. pbuff[0] = mt;
    113. count++;
    114. capcity = 2*count;
    115. }
    116. else
    117. {
    118. if(NULL != pbuff && count == capcity)
    119. {
    120. capcity *= 2;
    121. T *ptem = new T[capcity];
    122. size_t totalbytes = capcity*sizeof(T);
    123. memcpy(ptem,pbuff,totalbytes);
    124. del_buff();
    125. pbuff = ptem;
    126. pbuff[count] = mt;
    127. count ++;
    128. }
    129. if(NULL != pbuff && count != capcity)
    130. {
    131. pbuff[count] = mt;
    132. count++;
    133. }
    134. }
    135. }
    136. template<class T>
    137. typename MyVector<T>::iterator MyVector<T>::insert(size_t index,const T &mt)
    138. {
    139. assert(count >= index);
    140. if(NULL != pbuff && count == capcity)
    141. {
    142. capcity *= 2;
    143. T *ptem = new T[capcity];
    144. memcpy(ptem,pbuff,capcity*sizeof(T));
    145. ptem[index] = mt;
    146. memcpy(&ptem[index+1],(count-index)*sizeof(T));
    147. del_buff();
    148. pbuff = ptem;
    149. count ++;
    150. typename MyVector<T>::iterator _iter(&pbuff[index]);
    151. return _iter;
    152. }
    153. else if(NULL != pbuff && count != capcity)
    154. {
    155. size_t _end = count-1;
    156. size_t _beg = index;
    157. for(;_end >= _beg;_end--)
    158. pbuff[_end+1] = pbuff[_end];
    159. pbuff[index] = mt;
    160. count++;
    161. typename MyVector<T>::iterator _iter(&pbuff[index]);
    162. return _iter;
    163. }
    164. }
    165. template<class T>
    166. typename MyVector<T>::iterator MyVector<T>::
    167. insert(const T *phead,const T *pback,
    168. typename MyVector<T>::iterator p)
    169. {
    170. typename MyVector<T>::iterator _beg = begin(),_end = end();
    171. size_t insertnum = 0;
    172. for(;phead != pback;phead++)
    173. insertnum++;
    174. phead -= insertnum;
    175. size_t index = 0;
    176. for(;_beg != p;_beg++)
    177. index++;
    178. if(count +insertnum > capcity && NULL != pbuff)
    179. {
    180. capcity = 2*(count +insertnum);
    181. T *ptem = new T [capcity];
    182. memcpy(ptem,pbuff,(index)*sizeof(T));
    183. memcpy(&ptem[index],phead,insertnum*sizeof(T));
    184. memcpy(&ptem[index+insertnum],&pbuff[index],
    185. (count-index)*sizeof(T));
    186. del_buff();
    187. pbuff = ptem;
    188. count += insertnum;
    189. typename MyVector<T>::iterator _iter(&pbuff[index]);
    190. return _iter;
    191. }
    192. else if(count +insertnum <= capcity && NULL != pbuff)
    193. {
    194. for(size_t i = insertnum;i != 0;i--,count--)
    195. pbuff[count+insertnum-1] = pbuff[count-1];
    196. for(;phead != pback;phead++,p++)
    197. *p = *phead;
    198. count += insertnum;
    199. return p;
    200. }
    201. if(NULL == pbuff && 0 == count)
    202. {
    203. capcity = 2*insertnum;
    204. pbuff = new T[capcity];
    205. memcpy(pbuff,phead,insertnum*sizeof(T));
    206. count = insertnum;
    207. typename MyVector<T>::iterator _iter(&pbuff[0]);
    208. return _iter;
    209. }
    210. }
    211. template<class T>
    212. typename MyVector<T>::iterator MyVector<T>::erase(size_t index)
    213. {
    214. T *temp = new T[count-index-1];
    215. memcpy(temp,&pbuff[index+1],(count-index-1)*sizeof(T));
    216. memcpy(&pbuff[index],temp,(count-index-1)*sizeof(T));
    217. pbuff[count-1] = ‘\0‘;
    218. count--;
    219. delete [] temp;
    220. typename MyVector<T>::iterator _iter(&pbuff[index]);
    221. return _iter;
    222. }
    223. template<class T>
    224. typename MyVector<T>::iterator MyVector<T>::
    225. erase( typename MyVector<T>::iterator phead,
    226. typename MyVector<T>::iterator pback)
    227. {
    228. size_t elemnum = 0;
    229. size_t _toend = 0;
    230. for(;phead != pback;phead++)
    231. elemnum++;
    232. phead -= elemnum;
    233. for(;pback != end();pback++)
    234. _toend++;
    235. pback -= _toend;
    236. T *temp = new T[_toend];
    237. memcpy(temp,&pbuff[count-_toend],_toend*sizeof(T));
    238. memcpy(&pbuff[count-elemnum-_toend],temp,_toend*sizeof(T));
    239. memset(&pbuff[count-elemnum],‘\0‘,(count-elemnum)*sizeof(T));
    240. delete [] temp;
    241. count -= elemnum;
    242. return phead;
    243. }
    244. template<class T>
    245. typename MyVector<T>::iterator MyVector<T>::begin()
    246. {
    247. beg = &pbuff[0];
    248. return beg;
    249. }
    250. template<class T>
    251. typename MyVector<T>::iterator MyVector<T>::end()
    252. {
    253. last = &pbuff[count];
    254. return last;
    255. }
    256. /**nested dependent typeclass**/
    257. /**implementation**/
    258. template<class T>
    259. MyVector<T>::iterator::iterator(const typename MyVector<T>::iterator &iter)
    260. {
    261. pelem = iter.pelem;
    262. }
    263. template<class T>
    264. typename MyVector<T>::iterator& MyVector<T>::iterator::
    265. operator = (const typename MyVector<T>::iterator &iter)
    266. {
    267. pelem = iter.pelem;
    268. return *this;
    269. }
    270. template<class T>
    271. typename MyVector<T>::iterator& MyVector<T>::iterator::
    272. operator = (T *pt)
    273. {
    274. pelem = pt;
    275. return *this;
    276. }
    277. template<class T>
    278. bool MyVector<T>::iterator::operator !=
    279. (const typename MyVector<T>::iterator &iter)
    280. {
    281. if(pelem != iter.pelem)
    282. return true;
    283. else
    284. return false;
    285. }
    286. template<class T>
    287. typename MyVector<T>::iterator& MyVector<T>::iterator::operator ++ ()
    288. {
    289. ++pelem;
    290. return *this;
    291. }
    292. template<class T>
    293. typename MyVector<T>::iterator& MyVector<T>::iterator::operator ++ (int)
    294. {
    295. pelem++;
    296. return *this;
    297. }
    298. template<class T>
    299. typename MyVector<T>::iterator& MyVector<T>::iterator::operator -- (int)
    300. {
    301. pelem--;
    302. return *this;
    303. }
    304. template<class T>
    305. typename MyVector<T>::iterator& MyVector<T>::iterator::operator -- ()
    306. {
    307. --pelem;
    308. return *this;
    309. }
    310. template<class T>
    311. typename MyVector<T>::iterator& MyVector<T>::iterator::operator += (size_t size)
    312. {
    313. pelem += size;
    314. return *this;
    315. }
    316. template<class T>
    317. typename MyVector<T>::iterator& MyVector<T>::iterator::operator -= (size_t size)
    318. {
    319. pelem -= size;
    320. return *this;
    321. }
    322. template<class T>
    323. typename MyVector<T>::iterator MyVector<T>::iterator::operator + (size_t size)
    324. {
    325. pelem += size;
    326. typename MyVector<T>::iterator _iter(pelem);
    327. return _iter;
    328. }
    329. template<class T>
    330. typename MyVector<T>::iterator MyVector<T>::iterator::operator - (size_t size)
    331. {
    332. pelem -= size;
    333. typename MyVector<T>::iterator _iter(pelem);
    334. return _iter;
    335. }
    336. template<class T>
    337. T& MyVector<T>::iterator::operator * ()
    338. {
    339. return *pelem;
    340. }
    341. #endif
时间: 2024-07-31 10:04:31

Vector部分实现的相关文章

vector 学习笔记

vector 使用练习: /**************************************** * File Name: vector.cpp * Author: sky0917 * Created Time: 2014年04月27日 11:07:33 ****************************************/ #include <iostream> #include <vector> using namespace std; int main

初译 Support Vector Machines:A Simple Tutorial(一)

从本次开始我将开始尝试着逐章翻译一下 Alexey Nefedov的<Support Vector Machines:A Simple Tutorial>这本教材,这可是我们导师极力推荐的SVM教材,看了好久一直感觉一脸懵逼,索性开坑翻译一下吧,也当是加深理解,毕竟我也是一知半解,如果翻译的有不对的地方还望大佬们斧正,欢迎提意见,欢迎讨论. 嗯,就是这样. (一)Introduction 在本章节中将会介绍一些用于定义支持向量机(SVM)的基础的概念,这些概念对于理解SVM至关重要,假定读者了

Vector容器 和 iteration 迭代器

vector容器 vector是同一种类型的对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库负责管理存储元素的相关内存.我们把vector称为容器,是因为它可以包含其他对象.一个容器中的所有对象都必须是同一种类型的. 使用vector之前,必须包含相应的头文件.#include <vector> using std::vector; vector是一个类模板(class template).模板允许程序员编写单个类或函数定义,这个类和函数定义可用于不同的数据类型上.

C++ vector 使用

看vector 的使用: #include "stdafx.h" #include <iostream> #include <vector> using namespace std; int main() { vector<long> v; for (long i=0;i<10000;i++) { v.push_back(rand()); } cout << "size = " << v.size()

Vector Tile

Mapbox Vector Tile Specification A specification for encoding tiled vector data. <?XML:NAMESPACE PREFIX = "[default] http://www.w3.org/2000/svg" NS = "http://www.w3.org/2000/svg" />License The text of this specification is licens

vector,deque,list相关操作

1.vector的基本操作 (1).对动态数组元素的添加和删除.获取 代码如下: #include<iostream> #include<vector> using namespace std; //数组元素的添加和删除.获取 int main(void){     vector<int> v1;      v1.push_back(1);     v1.push_back(2);     v1.push_back(3);     cout<<"l

vector容器用法详解

vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组.像数组一样,vector类也用从0开始的下标表示元素的位置:但和数组不同的是,当vector对象创建后,数组的元素个数会随着vector对象元素个数的增大和缩小而自动变化. vector类常用的函数如下所示: 1.构造函数 vector():创建一个空vector vector(int nSize):创建一个vector,元素个数为nSize vector(int nSize,const t& t):创建一个vector,元

C++使用vector

#include <iostream> #include <string> #include <vector> using namespace std; void show(vector<int> v) { for(auto v1 : v){ cout << v1 << endl; } } void show2(vector<int> v) { for(vector<int>::const_iterator v

C++ STL学习——vector

学过C++的人肯定会很熟悉STL标准模板库,STL其实就是封装了一系列的接口,供我们调用.很多函数或者算法的实现不需要我们从头开始写,大大提高我们的编程效率.这篇博客在简单介绍STL的情况下,会详细的来介绍vector的使用. STL共有六大组件: 一.容器(Container):是一种数据结构,如list,vector,deque,queue等,以模板类的方法提供,为了访问容器中的数据,可以使用由容器类提供的迭代器. 二.迭代器(Iterator):提供了访问容器中对象的方法. 三.算法(Al

stl容器区别: vector list deque set map及底层实现

在STL中基本容器有: vector.list.deque.set.map set 和map都是无序的保存元素,只能通过它提供的接口对里面的元素进行访问 set :集合, 用来判断某一个元素是不是在一个组里面,使用的比较少 map :映射,相当于字典 ,把一个值映射成另一个值,如果想创建字典的话使用它好了 底层采用的是树型结构,多数使用平衡二叉树实现 ,查找某一值是常数时间,遍历起来效果也不错, 只是每次插入值的时候,会重新构成底层的平衡二叉树,效率有一定影响. vector.list.dequ