1、 C++模板实现队列 分类: 准备笔试2011-08-28 20:02 2088人阅读 评论(0) 收藏 举报 c++classvectorinclude 我准备练习一下模板的知识,然后自己实现vector类。在这之前,先用模板实现一个队列来热身吧。队列的底层是链表。主要是熟悉一下模板的写法。 另外,就是模板的定义和实现都要写在一个文件中(export关键字可以避免这样。还没用过),所以倒数第二行我加了个# include "queue.hpp",只能是hpp,不能是cpp。不然报错。我用的是4.5.2。 1.queue.h [cpp] view plaincopy 1. /
2、
2. * queue.h
3. *
4. * Created on: 2011-8-28
5. * Author: gauss
6. */
7.
8. #ifndef QUEUE_H_
9. #define QUEUE_H_
10.
11. template
3、类的声明形式
16. queue_item(const T& i) :
17. item(i), next(0) {
18. }
19. T item;
20. queue_item *next;
21. };
22.
23. template
4、 queue(const queue &q); 30. queue& operator=(const queue &q); 31. ~queue(); 32. void push(const T &i); 33. void pop(); 34. T front(); 35. T back(); 36. bool empty() { 37. if (n > 0) 38. return false; 39. else
5、
40. return true;
41. }
42. size_t size() {
43. return n;
44. }
45. void clear();
46.
47. private:
48. queue_item
6、3. #include "queue.hpp" //注意这句话。
54. #endif /* QUEUE_H_ */
2.queue.hpp
[cpp] view plaincopy
1. /*
2. * queue.hpp
3. *
4. * Created on: 2011-8-28
5. * Author: gauss
6. */
7.
8. #ifndef QUEUE_HPP_
9. #define QUEUE_HPP_
10.
11. template 7、name T>
12. void queue 8、
22. ++n;
23. }
24.
25. template 9、6. T queue 10、e T>
46. T queue 11、T>
56. void queue 12、0), n(0) {
70. copy_item(q);
71. }
72.
73. template 13、 }
82. return *this;
83. }
84.
85. template 14、QUEUE_HPP_ */
3.main.cpp
[cpp] view plaincopy
1. #include 15、 if (q.empty())
12. cout << "empty" << endl;
13. else
14. cout << "not empty" << endl;
15. q.push(1);
16. q.push(2);
17. queue 16、22. cout << "not empty" << endl;
23. cout << "queue 2: " << q2.front() << endl;
24. cout << "queue 2: " << q2.back() << endl;
25. cout << "the size of queue 2: " << q2.size() << endl;
26. q = q2;
27. cout << "queue 1: " << q.front() << endl;
28. cout 17、 << "queue 1: " << q.back() << endl;
29.
30. queue 18、
empty
queue 2: 1
queue 2: 2
the size of queue 2: 2
queue 1: 1
queue 1: 2
gauss
jiawenjie
读C++ Primer 之队列类模板
分类: c/c++2011-08-08 20:33 1575人阅读 评论(0) 收藏 举报
c++classmakefiledatenull语言
在C语言学习阶段,我们学过了队列的C语言的实现,但是在C++里面,我们使用泛型实现了实现了队列类的模板,虽然有现成的队列类的模板,但是这对于我们了解怎样用泛型以及泛型编程中的一些需要注意的地方却是受益匪浅, 19、特别是容易忘记的地方。
好了,接下来咱们上代码
首先是 templateclass.h 文件的实现
1. /*
2. * author:xizero00
3. * mail:xizero00@
4. * date:2011-08-08 01:34:33
5. * Template Queue Sample 模板队列示例
6. */
7.
8.
9. #ifndef TEMPLATECLASS_H
10. #define TEMPLATECLASS_H
11.
12. #include 20、
14. using namespace std;
15.
16.
17. //Queue的实现类
18. //这里是特定的模板友元关系,即类可以只授权特定的实例的访问权,即类Queue和类QueueItem是一对一的关系
19. template 21、lass Queue 22、
39. class Queue
40. {
41. public:
42. //需要注意的一点是,一般在类中实现的函数都会是内联函数
43. //使用内联函数的要求是,该函数的代码一般只有几行,并且经常执行
44.
45.
46.
47. //默认构造函数
48.
49. //这样的声明也是可以的
50. //Queue() :head( 0 ) , tail( 0 ) {}
51. Queue 23、 tail( 0 ) {}
52.
53.
54.
55. //复制构造函数
56.
57. //当然这样的声明也是可以的
58. //Queue( const Queue &q ): head( q.head ) , tail( q.tail ) { copy_elems( q ); }
59. Queue 24、
61.
62.
63. //操作符重载
64. Queue 25、nst版本的
75. //const T& front() { return head->item; }
76.
77.
78.
79.
80.
81. //入队列
82. void push( const T& );
83.
84. //出队列
85. void pop();
86.
87. //判断是否为空
88. bool empty() const { return NULL = 26、 head; }
89.
90.
91. //显示队列元素
92. void ShowElements() ;
93.
94.
95.
96. private:
97. //队列的头指针,尾指针,主要用于出入队列用
98. QueueItem 27、103.
104. //复制元素
105. void copy_elems( const Queue& );
106. };
107.
108. //出队列,即删除队列头部的元素
109. template 28、117.
118. //保存当前指针的值
119. QueueItem 29、h( const T &t )
132. {
133. //构造一个对象
134. QueueItem 30、尾指针的指向下一个元素的指针指向生成的数据
145. tail->next = p;
146.
147. //将尾指针移动到最后一个数据上去
148. tail = p;
149. }
150. }
151.
152.
153.
154. //销毁数据
155. template 31、 while( !empty() )
160. {
161. pop();
162. }
163. }
164.
165.
166. //赋值操作符重载
167. template 32、p = p->next )
172. {
173. push( p->item );
174. }
175.
176. }
177.
178.
179. template 33、当前队列为空...." << endl;
186. }
187.
188.
189. for( QueueItem 34、类的声明中也不要省略 35、
11. {
12. Queue 36、ue 37、<< endl;
36. p.pop();
37. p.pop();
38. p.pop();
39. p.ShowElements();
40.
41.
42.
43.
44. return 0;
45. }
下面给出编译所需的Makefile
1. # author:xizero00
2. # mail:xizero00@
3. # date:2011-08-08 01:37:39
4. # Makefile for T 38、emplate Queue Sample 模板队列示例
5.
6. compile:
7. g++ templatesample.cc -g -o template
8. ./template
9. clean:
10. rm -f template
11. ls -al template*
下面给出我的运行结果:
1. q队列元素如下:
2. 当前队列元素为:
3. 1
4. 2
5. 4
6. 5
7. p队列元素如下(它是复制的q队列的元素):
8. 当前队列元素为:
9. 1
10. 2
11. 4
12. 5
13. 对p队列进行出队列操作
14. 当前队列元素为:
15. 2
16. 4
17. 5
18. 出队列操作进行3次
19. 当前队列元素为:
20. oops,当前队列为空....
总结:在学习模板类的时候,我想最重要的还是需要动手,其次是养成良好的编码习惯,能够让你的注释清晰易懂,让你的理解也能够让别人理解,代码看起来清清爽爽的才是最终目的,学习的目的就是和他们分享,一起进步!






