码迷,mamicode.com
首页 > 其他好文 > 详细

模板参数

时间:2016-03-26 12:41:55      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:capacity   include   private   public   

  1. 用模板函数实现stack


  2. #include <iostream>

  3. using namespace std;

  4. template<typename T>

  5. class Seqlist

  6. {

  7. private:

  8. T *_data;

  9. int _size;

  10. int _capacity;

  11. public:

  12. Seqlist()

  13. :_size(0), _capacity(3), _data(0)

  14. {

  15. _data = new T[_capacity];

  16. }

  17. ~Seqlist()

  18. {

  19. if (_data != NULL)

  20. delete[] _data;

  21. _data = NULL;

  22. }

  23. void Checkcapacity()

  24. {

  25. if (_size == _capacity)

  26. {

  27. T *tmp = new T[2 * _capacity + 3];

  28. memcpy(tmp, _data, _size*sizeof(T));

  29. delete _data;

  30. _data = NULL;

  31. _data = tmp;

  32. _capacity = 2 * _capacity + 3;

  33. }

  34. }

  35. Seqlist(const Seqlist&s)

  36. {

  37. _data = new T[s._capacity];

  38. _size = s._size;

  39. _capacity = s._capacity;

  40. memcpy(_data, s._data, _capacity*sizeof(T));


  41. }

  42. Seqlist &operator=(const Seqlist&s)

  43. {

  44. if (this != &s)

  45. {

  46. delete[]_data;

  47. _data = new T[s._capacity];

  48. _size = s._size;

  49. _capacity = s._capacity;

  50. memcpy(_data, s._data, _capacity*sizeof(T));

  51. }

  52. return *this;

  53. }

  54. void PushBack(const T&d)

  55. {

  56. Checkcapacity();

  57. *(_data + _size) = d;

  58. _size++;

  59. }

  60. void  PopBack()

  61. {

  62. _size--;

  63. }

  64. void PushFront(const T&d)

  65. {


  66. Checkcapacity();

  67. int i = 0;

  68. for (i = _size; i > 0; i--)

  69. {

  70. *(_data + i) = *(_data + i - 1);

  71. }

  72. *_data = d;

  73. _size++;

  74. }

  75. void PopFront()

  76. {

  77. int i = 0;

  78. for (i = 0; i < _size - 1; i++)

  79. *(_data + i) = *(_data + i + 1);

  80. _size--;

  81. }

  82. void Insert(int pos, const T&d);

  83. void Sort()

  84. {

  85. T tmp = 0;

  86. int i = 0;

  87. int j = 0;

  88. for (i = 0; i < _size - 1; i++)

  89. for (j = i + 1; j < _size; j++)

  90. {

  91. if (*(_data + i) >*(_data + j))

  92. {

  93. tmp = *(_data + i);

  94. *(_data + i) = *(_data + j);

  95. *(_data + j) = tmp;

  96. }

  97. }

  98. }

  99. void print()

  100. {

  101. int i = 0;

  102. for (i = 0; i < _size; i++)

  103. cout << *(_data + i) << " ";

  104. }


  105. };

  106. template<typename T>

  107. void Seqlist<T>::Insert(int pos, const T&d)

  108. {

  109. Checkcapacity();

  110. int i = 0;

  111. for (i = _size; i>pos; i--)

  112. *(_data + i) = *(_data + i - 1);

  113. *(_data + pos) = d;

  114. _size++;

  115. }

  116. template <typename T,typename Container=Seqlist <T> >

  117. class Stack

  118. {

  119. private:

  120. Container con;

  121. public:

  122. void Push(const T&d)

  123. {

  124. con.PushBack(d);

  125. }

  126. void Pop()

  127. {

  128. con.PopBack();

  129. }

  130. void print()

  131. {

  132. con.print();

  133. }

  134. };

  135. int main()

  136. {

  137. Stack<int, Seqlist<int> >stack1;

  138. stack1.Push(1);

  139. stack1.Push(2);

  140. stack1.Push(3);

  141. stack1.Push(4);

  142. stack1.print();

  143. getchar();

  144. return 0;

  145. }

  146. 2.运用stack

  147. #include <iostream>

  148. #include <stack>

  149. using namespace std;

  150. int main()

  151. {

  152. stack<int>s;

  153. s.push(1);

  154. s.push(2);

  155. s.push(3);

  156. return 0;

  157. }

  158. 3.模板的模板参数

  159. template<typename T,template<class> class Container=Seqlist >

  160. class Stack

  161. {

  162. private:

  163. Container<T> con;

  164. public:

  165. void Push(const T&d)

  166. {

  167. con.PushBack(d);

  168. }

  169. void Pop()

  170. {

  171. con.PopBack();

  172. }

  173. void print()

  174. {

  175. con.print();

  176. }

  177. };

  178. int main()

  179. {

  180. Stack<int>s;

  181. s.Push(1);

  182. s.Push(2);

  183. s.Push(3);

  184. s.print();

  185. getchar();

  186. return 0;

  187. }

  188. 4.非类型的类模板参数

  189. template <typename T,int MAX_SIZE=10>

  190. class Seqlist

  191. {

  192. private:

  193. T data[MAX_SIZE];

  194. //public:

  195. // void show( int a)

  196. // {

  197. // cout << "show()" << endl;

  198. // }

  199. };

  200. //template <typename T, int MAX_SIZE = 10>

  201. //void Seqlist<T, MAX_SIZE>::show(int a)

  202. //{

  203. // cout << "show()" << endl;

  204. //}

  205. int main()

  206. {

  207. Seqlist<int, 10>s1;

  208. Seqlist <int, 20>s2;

  209. getchar();

  210. return 0;

  211. }

  212. 5.非类型的模板函数参数

  213. template <typename T,int val>

  214.  T add(const T& x)

  215. {

  216. return x + val;

  217. }

  218. int main()

  219. {

  220.   int ret = add<int, 3>(1);

  221. cout << ret << endl;

  222. getchar();

  223. return 0;

  224. }

模板参数

标签:capacity   include   private   public   

原文地址:http://10810512.blog.51cto.com/10800512/1755412

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!