• day47:C++ day7,异常处理、using的第三种用法、类型转换、lambda表达式、STL标准模板库


    my_vectoers.h:

    1. #ifndef MY_VECTORS_H
    2. #define MY_VECTORS_H
    3. #include
    4. using namespace std;
    5. template<typename TYPE>
    6. class my_vectors
    7. {
    8. private:
    9. TYPE* ptr;
    10. int num;
    11. int cnum;
    12. TYPE* start_ptr=NULL;
    13. TYPE* end_ptr=NULL;
    14. public:
    15. //无参构造
    16. my_vectors(){};
    17. //有参构造
    18. my_vectors(int num,const TYPE &val1);
    19. //拷贝构造
    20. my_vectors(const my_vectors &L);
    21. //构造2
    22. my_vectors(TYPE* start,TYPE* end);
    23. //=运算符重载
    24. bool operator=(const my_vectors &L);
    25. //>运算符重载
    26. bool operator>(const my_vectors &L);
    27. //[]运算符重载
    28. TYPE & operator[](int index);
    29. //assign函数1
    30. void assign(TYPE* start,TYPE* end);
    31. //assign函数2
    32. void assign( int num, const TYPE &val2 );
    33. //at函数
    34. TYPE at( int pos );
    35. //back函数
    36. TYPE back();
    37. //伪begin函数
    38. TYPE* begin();
    39. //capacity 函数
    40. int capacity();
    41. //clear 函数
    42. void clear();
    43. //empty 函数
    44. bool empty();
    45. //full 函数
    46. bool full();
    47. //伪end函数
    48. TYPE* end();
    49. //front函数
    50. TYPE front();
    51. //insert函数1
    52. TYPE* insert( int loc, const TYPE &val );
    53. //insert函数2
    54. void insert( int loc, int num, const TYPE &val );
    55. //insert函数3
    56. void insert( int loc, TYPE* start, TYPE* end );
    57. //max_size 函数
    58. int max_size();
    59. //pop_back函数
    60. void pop_back();
    61. //push_back 函数
    62. void push_back( const TYPE &val );
    63. //伪rbegin 函数
    64. TYPE* rbegin();
    65. //伪rend 函数
    66. TYPE* rend();
    67. //resize 函数
    68. void resize( int size, TYPE val );
    69. //size 函数
    70. int size();
    71. //swap 函数
    72. void swap( my_vectors &from );
    73. //三杯水
    74. void swap(TYPE &a,TYPE &b);
    75. //show函数
    76. void show();
    77. };
    78. #endif // MY_VECTORS_H

    my_vector.cpp:

    1. #include "my_vectors.h"
    2. //有参构造
    3. template<typename TYPE>
    4. my_vectors::my_vectors(int num,const TYPE &val1)
    5. {
    6. this->num=num;
    7. cnum=num;
    8. ptr=new TYPE(cnum);
    9. for(int i=0;i
    10. {
    11. ptr[i]=val1;
    12. }
    13. start_ptr=ptr;
    14. end_ptr=ptr+num-1;
    15. }
    16. //拷贝构造
    17. template<typename TYPE>
    18. my_vectors::my_vectors(const my_vectors &L)
    19. {
    20. this->num=L.num;
    21. cnum=num;
    22. ptr=new TYPE(cnum);
    23. memcpy(this,&L,sizeof(my_vectors));
    24. memcpy(ptr,L.ptr,sizeof(TYPE)*num);
    25. start_ptr=ptr;
    26. end_ptr=ptr+num-1;
    27. }
    28. //构造2
    29. template<typename TYPE>
    30. my_vectors::my_vectors(TYPE* start,TYPE* end)
    31. {
    32. num=(end-start)/sizeof(TYPE);
    33. cnum=num;
    34. ptr=new TYPE(cnum);
    35. memcpy(ptr,start,sizeof(TYPE)*num);
    36. start_ptr=ptr;
    37. end_ptr=ptr+num-1;
    38. }
    39. //=运算符重载
    40. template<typename TYPE>
    41. bool my_vectors::operator=(const my_vectors &L)
    42. {
    43. if(num == L.num && memcmp(ptr,L.ptr,sizeof(num)) == 0)
    44. {
    45. return true;
    46. }
    47. return false;
    48. }
    49. //>运算符重载
    50. template<typename TYPE>
    51. bool my_vectors::operator>(const my_vectors &L)
    52. {
    53. if(num > L.num)
    54. {
    55. return true;
    56. }else if(num == L.num && memcmp(ptr,L.ptr,sizeof(num)) > 0)
    57. {
    58. return true;
    59. }else{
    60. return false;
    61. }
    62. }
    63. //[]运算符重载
    64. template<typename TYPE>
    65. TYPE &my_vectors::operator[](int index)
    66. {
    67. return *(ptr+index);
    68. }
    69. //assign函数1
    70. template<typename TYPE>
    71. void my_vectors::assign(TYPE* start,TYPE* end)
    72. {
    73. delete []ptr;
    74. num=(end-start)/sizeof(TYPE);
    75. cnum=num;
    76. ptr=new TYPE(cnum);
    77. memcpy(ptr,start,sizeof(TYPE)*num);
    78. start_ptr=ptr;
    79. end_ptr=ptr+num-1;
    80. }
    81. //assign函数2
    82. template<typename TYPE>
    83. void my_vectors::assign( int num, const TYPE &val2 )
    84. {
    85. delete []ptr;
    86. this->num=num;
    87. cnum=num;
    88. ptr=new TYPE(cnum);
    89. for( int i=0;i
    90. {
    91. ptr[i]=val2;
    92. }
    93. start_ptr=ptr;
    94. end_ptr=ptr+num-1;
    95. }
    96. //at函数
    97. template<typename TYPE>
    98. TYPE my_vectors::at( int pos )
    99. {
    100. if(pos>=0&&pos
    101. {
    102. return ptr[pos];
    103. }
    104. else{
    105. cout<<"pos error";
    106. }
    107. }
    108. //back函数
    109. template<typename TYPE>
    110. TYPE my_vectors::back()
    111. {
    112. return *end_ptr;
    113. }
    114. //伪begin函数
    115. template<typename TYPE>
    116. TYPE* my_vectors::begin()
    117. {
    118. return start_ptr;
    119. }
    120. //capacity 函数
    121. template<typename TYPE>
    122. int my_vectors::capacity()
    123. {
    124. return cnum;
    125. }
    126. //clear 函数
    127. template<typename TYPE>
    128. void my_vectors::clear()
    129. {
    130. while(!empty())
    131. {
    132. pop_back();
    133. }
    134. }
    135. //empty 函数
    136. template<typename TYPE>
    137. bool my_vectors::empty()
    138. {
    139. return start_ptr==NULL&&end_ptr==NULL;
    140. }
    141. //full 函数
    142. template<typename TYPE>
    143. bool my_vectors::full()
    144. {
    145. return cnum==num;
    146. }
    147. //伪end函数
    148. template<typename TYPE>
    149. TYPE* my_vectors::end()
    150. {
    151. return end_ptr;
    152. }
    153. //front函数
    154. template<typename TYPE>
    155. TYPE my_vectors::front()
    156. {
    157. return &ptr[0];
    158. }
    159. //insert函数1
    160. template<typename TYPE>
    161. TYPE* my_vectors::insert( int loc, const TYPE &val )
    162. {
    163. if(full())
    164. {
    165. cnum*=2;
    166. TYPE s[num];
    167. memcpy(s,this,sizeof(TYPE)*num);
    168. delete []ptr;
    169. ptr=new TYPE(cnum);
    170. memcpy(ptr,s,sizeof(TYPE)*num);
    171. }
    172. for(int i=ptr+num+1;i>=ptr+loc-1;i--)
    173. {
    174. *(ptr+i)=*(ptr+i-1);
    175. }
    176. *(ptr+loc-1)=val;
    177. num++;
    178. return ptr+loc-1;
    179. }
    180. //insert函数2
    181. template<typename TYPE>
    182. void my_vectors::insert( int loc, int num, const TYPE &val )
    183. {
    184. if(full())
    185. {
    186. while(cnum<this->num+num)
    187. {
    188. cnum*=2;
    189. }
    190. TYPE s[num];
    191. memcpy(s,this,sizeof(TYPE)*num);
    192. delete []ptr;
    193. ptr=new TYPE(cnum);
    194. memcpy(ptr,s,sizeof(TYPE)*num);
    195. }
    196. for(int i=loc-1;i
    197. {
    198. *ptr[i]=val;
    199. }
    200. num++;
    201. }
    202. //insert函数3
    203. template<typename TYPE>
    204. void my_vectors::insert( int loc, TYPE* start, TYPE* end )
    205. {
    206. int size=(end-start)/sizeof(TYPE);
    207. if(full())
    208. {
    209. while(cnum<this->num+size)
    210. {
    211. cnum*=2;
    212. }
    213. TYPE s[num];
    214. memcpy(s,this,sizeof(TYPE)*num);
    215. delete []ptr;
    216. ptr=new TYPE(cnum);
    217. memcpy(ptr,s,sizeof(TYPE)*num);
    218. }
    219. TYPE s1[num];
    220. memcpy(s1,ptr+loc-1,sizeof(TYPE)*(num-loc));
    221. memcpy(ptr+loc-1,start,sizeof(TYPE)*size);
    222. memcpy(ptr+loc-1+size,s1,sizeof(TYPE)*(num-loc));
    223. num++;
    224. }
    225. //max_size 函数
    226. template<typename TYPE>
    227. int my_vectors::max_size()
    228. {
    229. return cnum;
    230. }
    231. //pop_back函数
    232. template<typename TYPE>
    233. void my_vectors::pop_back()
    234. {
    235. if(!empty())
    236. {
    237. num--;
    238. if(num==0)
    239. {
    240. start_ptr=NULL;
    241. end_ptr=NULL;
    242. }
    243. }
    244. }
    245. //push_back 函数
    246. template<typename TYPE>
    247. void my_vectors::push_back( const TYPE &val )
    248. {
    249. if(full())
    250. {
    251. cnum*=2;
    252. TYPE s[num];
    253. memcpy(s,this,sizeof(TYPE)*num);
    254. delete []ptr;
    255. ptr=new TYPE(cnum);
    256. memcpy(ptr,s,sizeof(TYPE)*num);
    257. }
    258. *(ptr+num)=val;
    259. end_ptr++;
    260. num++;
    261. }
    262. //伪rbegin 函数
    263. template<typename TYPE>
    264. TYPE* my_vectors::rbegin()
    265. {
    266. return end_ptr;
    267. }
    268. //伪rend 函数
    269. template<typename TYPE>
    270. TYPE* my_vectors::rend()
    271. {
    272. return start_ptr;
    273. }
    274. //resize 函数
    275. template<typename TYPE>
    276. void my_vectors::resize( int size, TYPE val )
    277. {
    278. delete[] ptr;
    279. ptr=new TYPE(size);
    280. num=size;
    281. cnum=size;
    282. for(int i=0;i
    283. {
    284. *(ptr+i)=val;
    285. }
    286. start_ptr=ptr;
    287. end_ptr=ptr+size-1;
    288. }
    289. //size 函数
    290. template<typename TYPE>
    291. int my_vectors::size()
    292. {
    293. return num;
    294. }
    295. //swap 函数
    296. template<typename TYPE>
    297. void my_vectors::swap( my_vectors &from )
    298. {
    299. swap(this->ptr,from.ptr);
    300. swap(this->num,from.num);
    301. swap(this->cnum,from.cnum);
    302. swap(this->start_ptr,from.start_ptr);
    303. swap(this->end_ptr,from.end_ptr);
    304. }
    305. //三杯水
    306. template<typename TYPE>
    307. void my_vectors::swap(TYPE &a,TYPE &b)
    308. {
    309. TYPE temp;
    310. temp=a;
    311. a=b;
    312. b=temp;
    313. }
    314. //show函数
    315. template<typename TYPE>
    316. void my_vectors::show()
    317. {
    318. for(int i=0;i
    319. {
    320. cout<<*(ptr+i)<<"\t";
    321. }
    322. cout<
    323. }

    main.c:

    1. #include "my_vectors.cpp"
    2. int main()
    3. {
    4. my_vectors<int> a;
    5. my_vectors<int>b(5,9);
    6. cout<empty()<
    7. cout<full()<
    8. b.show();
    9. b.pop_back();
    10. b.pop_back();
    11. b.push_back(55);
    12. b.push_back(55);
    13. b.show();
    14. cout<at(3)<
    15. b.clear();
    16. cout<empty()<
    17. return 0;
    18. }

    思维导图有道云笔记

  • 相关阅读:
    机器学习笔记之指数族分布——最大熵原理与softmax激活函数的关系
    S1_servlet与数据库连接、filter过滤器、分页 实操的叙述
    敏捷已死?
    【ACWing】123. 士兵
    uniapp登录逻辑
    竞赛选题 深度学习 机器视觉 人脸识别系统 - opencv python
    JSP SSM 成果展示系统myeclipse开发mysql数据库springMVC模式java编程计算机网页设计
    go.sum文件的作用
    VsCode中设置文本格式化(超详细)
    脊髓型颈椎病是什么?
  • 原文地址:https://blog.csdn.net/wxmchong/article/details/132891656