Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97537
  • 博文数量: 21
  • 博客积分: 145
  • 博客等级: 入伍新兵
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-22 17:37
文章分类

全部博文(21)

文章存档

2013年(16)

2012年(5)

我的朋友

分类: C/C++

2012-12-22 18:06:30

最近突然想学习一下C++,就找了一本《C++编程思想》来看,感觉课后习题练手没有
什么感觉,就又找了这本《数据结构与STL》,发现每章后面都有些实验和小项目,练
手蛮不错的,随便也可以试着学习一下STL和模板,为进一步深入学习做准备。

Chapter 2
这一章主要是介绍性的,所以随便看了看,就开始做课后的实验。
#实现一个Linked list,支持简单的Iterator功能

点击(此处)折叠或打开

  1. /*
  2.  * A brief implementation of liked list,
  3.  * which supports iterator.
  4.  */

  5. template <class T>
  6. class Linked {
  7. protected:
  8.     struct Node {
  9.         T ele;
  10.         Node *next;
  11.         
  12.         Node(): next(NULL) {}
  13.         Node(const T &x): ele(x), next(NULL) {}
  14.     };

  15.     size_t length; // linked list length
  16.     Node *head;
  17.     Node *tail;

  18. public:
  19.     class Iterator{
  20.     private:
  21.         Node *nodePtr;
  22.     public:
  23.         Iterator(Node* newPtr = NULL): nodePtr(newPtr) {}
  24.         Iterator operator++(int); // iterator++
  25.         Iterator operator=(const Iterator&);
  26.         Iterator operator++(); // ++iterator
  27.         T operator*() const;
  28.         bool operator==(const Iterator&) const;
  29.         bool operator!=(const Iterator&) const;
  30.     };

  31.     Linked() {
  32.         tail = new Node();
  33.         head = tail;
  34.         length = 0;
  35.     }
  36.     ~Linked() {
  37.         while (head != tail)
  38.             pop_front();
  39.         delete tail;
  40.     }
  41.     inline size_t size() const;
  42.     void push_front(const T& item);
  43.     void front(T& item) const;
  44.     void pop_front();
  45.     inline bool empty() const;
  46.     inline Iterator begin() const;
  47.     inline Iterator end() const;

  48. };

为了尝试一下模板,我故意把一些函数的实现放在了类的外面,结果返回Iterator的函数
老是报错:没有相关的constructor,deconstructor啥的

点击(此处)折叠或打开

  1. template <class T>
  2. Linked<T>::Iterator Linked<T>::Iterator::operator++() {
  3.     nodePtr = nodePtr->next;
  4.     return *this;
  5. }

  6. //在Linked<T>::Iterator前加一个class,就可以了。
  7. template <class T>
  8. class Linked<T>::Iterator Linked<T>::Iterator::operator++() {
  9.     nodePtr = nodePtr->next;
  10.     return *this;
  11. }
百度了一下,“依赖受限名称不能作为类型使用,除非在前面加上 typename关键字“
具体原理,先不管了,现在这不是重点。就先把雷埋在这里吧,呵呵。

下面是一个简单的测试:

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <string>

  3. #include "Linked.h"

  4. using namespace std;

  5. int main() {
  6.     Linked<string> strList;

  7.     string str;
  8.     cout << "Input 10 words" << endl;
  9.     for (int i=0; i<10; i++) {
  10.         cin >> str;
  11.         strList.push_front(str);
  12.     }
  13.     cout << "strList.size: " << strList.size() << endl;
  14.     strList.front(str);
  15.     strList.pop_front();
  16.     strList.front(str);
  17.     cout << "Pop the top, the new front is: " << str << endl;
  18.     cout << "New size: " << strList.size() << endl;
  19.     cout << "Go through list with iterator: " << endl;
  20.     Linked<string>::Iterator itr;
  21.     for (itr = strList.begin(); itr != strList.end(); itr++)
  22.         cout << *itr << " ";
  23.     cout << endl;
  24.     itr = strList.begin();
  25.     ++itr;
  26.     cout << "The second word: " << *itr << endl;
  27.     return 0;
  28. }

总结一下,感觉自己对C++太不熟悉,这个实验中至少有以下几点不懂:
1) 运算符重载函数返回类型,是引用还是值呢?
2)返回过程中的拷贝构造,还有赋值方面,都疑云重重。
3)模板相当不懂啊。
希望大家能给点意见给我这个悲哀的后进者,谢谢。

最后给出完整代码:

点击(此处)折叠或打开

  1. #ifndef LINKED_H
  2. #define LINKED_H

  3. #include <cassert>

  4. /*
  5.  * A brief implementation of liked list,
  6.  * which supports iterator.
  7.  */

  8. template <class T>
  9. class Linked {
  10. protected:
  11.     struct Node {
  12.         T ele;
  13.         Node *next;
  14.         
  15.         Node(): next(NULL) {}
  16.         Node(const T &x): ele(x), next(NULL) {}
  17.     };

  18.     size_t length; // linked list length
  19.     Node *head;
  20.     Node *tail;

  21. public:
  22.     class Iterator{
  23.     private:
  24.         Node *nodePtr;
  25.     public:
  26.         Iterator(Node* newPtr = NULL): nodePtr(newPtr) {}
  27.         Iterator operator++(int); // iterator++
  28.         Iterator operator=(const Iterator&);
  29.         Iterator operator++(); // ++iterator
  30.         T operator*() const;
  31.         bool operator==(const Iterator&) const;
  32.         bool operator!=(const Iterator&) const;
  33.     };

  34.     Linked() {
  35.         tail = new Node();
  36.         head = tail;
  37.         length = 0;
  38.     }
  39.     ~Linked() {
  40.         while (head != tail)
  41.             pop_front();
  42.         delete tail;
  43.     }
  44.     inline size_t size() const;
  45.     void push_front(const T& item);
  46.     void front(T& item) const;
  47.     void pop_front();
  48.     inline bool empty() const;
  49.     inline Iterator begin() const;
  50.     inline Iterator end() const;

  51. };

  52. template <class T>
  53. bool Linked<T>::Iterator::operator!=(const Iterator &itr) const {
  54.     if (nodePtr != itr.nodePtr)
  55.         return true;
  56.     return false;
  57. }

  58. template <class T>
  59. bool Linked<T>::Iterator::operator==(const Iterator &itr) const {
  60.     if (nodePtr == itr.nodePtr)
  61.         return true;
  62.     return false;
  63. }

  64. template <class T>
  65. T Linked<T>::Iterator::operator*() const {
  66.     return nodePtr->ele;
  67. }

  68. template <class T>
  69. class Linked<T>::Iterator Linked<T>::Iterator::operator++() {
  70.     nodePtr = nodePtr->next;
  71.     return *this;
  72. }

  73. template <class T>
  74. class Linked<T>::Iterator Linked<T>::Iterator::operator=(const Iterator &itr) {
  75.     if (this != &itr) {
  76.         nodePtr = itr.nodePtr;
  77.     }
  78.     return *this;
  79. }

  80. template <class T>
  81. class Linked<T>::Iterator Linked<T>::Iterator::operator++(int x) {
  82.     Iterator temp = *this;
  83.     nodePtr = nodePtr->next;
  84.     return temp;
  85. }

  86. template <class T>
  87. class Linked<T>::Iterator Linked<T>::begin() const {
  88.     return Iterator(head);
  89. }

  90. template <class T>
  91. class Linked<T>::Iterator Linked<T>::end() const {
  92.     return Iterator(tail);
  93. }

  94. template <class T>
  95. bool Linked<T>::empty() const {
  96.     if (length > 0)
  97.         return true;
  98.     else
  99.         return false;
  100. }

  101. template <class T>
  102. void Linked<T>::pop_front() {
  103.     assert(length > 0);
  104.     Node *oldPtr = head;
  105.     head = head->next;
  106.     delete oldPtr;
  107.     length--;
  108. }

  109. template <class T>
  110. void Linked<T>::front(T &item) const {
  111.     assert(length > 0);
  112.     item = head->ele;
  113. }

  114. template <class T>
  115. void Linked<T>::push_front(const T &item) {
  116.     Node *newPtr = new Node(item);
  117.     newPtr->next = head;
  118.     head = newPtr;
  119.     length++;
  120. }

  121. template <class T>
  122. size_t Linked<T>::size() const {
  123.     return length;
  124. }

  125. #endif // LINKED_H
阅读(264) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:《数据结构与STL》笔记和习题(2)

给主人留下些什么吧!~~