代码有问题 https://bbs.csdn.net/topics/390066863
根据这个改了代码,之后可以运行了
重复具有动态长度Stack类的过程
这里的Stack类带有一个嵌套的迭代器
//: C16:TStack2.h
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Templatized Stack with nested iterator
#ifndef TSTACK2_H
#define TSTACK2_H
template<class T> class Stack {
struct Link {
T* data;
Link* next;
Link(T* dat, Link* nxt)
: data(dat), next(nxt) {}
}* head;
public:
Stack() : head(0) {}
~Stack();
void push(T* dat) {
head = new Link(dat, head);
}
T* peek() const {
return head ? head->data : 0;
}
T* pop();
// Nested iterator class:
class iterator; // Declaration required
friend class iterator; // Make it a friend
class iterator { // Now define it
typename Stack::Link* p;
public:
iterator(const Stack<T>& tl) : p(tl.head) {}
// Copy-constructor:
iterator(const iterator& tl) : p(tl.p) {}
// The end sentinel iterator:
iterator() : p(0) {}
// operator++ returns boolean indicating end:
bool operator++() {
if(p->next)
p = p->next;
else p = 0; // Indicates end of list
return bool(p);
}
bool operator++(int) { return operator++(); }
T* current() const {
if(!p) return 0;
return p->data;
}
// Pointer dereference operator:
T* operator->() const {
require(p != 0,
"PStack::iterator::operator->returns 0");
return current();
}
T* operator*() const { return current(); }
// bool conversion for conditional test:
operator bool() const { return bool(p); }
// Comparison to test for end:
bool operator==(const iterator&) const {
return p == 0;
}
bool operator!=(const iterator&) const {
return p != 0;
}
};
iterator begin() const {
return iterator(*this);
}
iterator end() const { return iterator(); }
};
template<class T> Stack<T>::~Stack() {
while(head)
delete pop();
}
template<class T> T* Stack<T>::pop() {
if(head == 0) return 0;
T* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;
}
#endif // TSTACK2_H ///:~
我们已经注意到,这个类已经被修改以支持所有权,它能工作是因为这个类
知道确切类型
迭代器是简单的,体积非常小,即单个指针的大小
当创建一个迭代器时,它被初始化为指向链表的头,只能沿着链表向前递增
为了对由迭代器指向的对象调用函数,我们可以使用current()函数,operator*
和指针反向引用operator-> 迭代器中的一个共同点
class iterator 嵌套在容器内,包括构造函数,可以创建指向容器中的一个元素
的一个迭代器和一个 终止哨兵 迭代器检验迭代器的测试
//: C16:TStack2Test.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
#include "TStack2.h"
#include "../require.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream file("TStack2Test.cpp");
assure(file, "TStack2Test.cpp");
Stack<string> textlines;
// Read file and store lines in the Stack:
string line;
while(getline(file, line))
textlines.push(new string(line));
int i = 0;
// Use iterator to print lines from the list:
Stack<string>::iterator it = textlines.begin();
Stack<string>::iterator* it2 = 0;
while(it != textlines.end()) {
cout << it->c_str() << endl;
it++;
if(++i == 10) // Remember 10th line
it2 = new Stack<string>::iterator(it);
}
cout << (*it2)->c_str() << endl;
delete it2;
getchar();
} ///:~
Stack是一个示例,用来存放string对象,并用来自一个文件的行填充
然后创建一个迭代器,用于遍历这个序列
输出
} ///:~
delete it2;
cout << (*it2)->c_str() << endl;
}
it2 = new Stack<string>::iterator(it);
if(++i == 10) // Remember 10th line
it++;
cout << it->c_str() << endl;
while(it != textlines.end()) {
Stack<string>::iterator* it2 = 0;
Stack<string>::iterator it = textlines.begin();
// Use iterator to print lines from the list:
int i = 0;
textlines.push(new string(line));
while(getline(file, line))
string line;
// Read file and store lines in the Stack:
Stack<string> textlines;
assure(file, "TStack2Test.cpp");
ifstream file("TStack2Test.cpp");
int main() {
using namespace std;
#include <string>
#include <fstream>
#include <iostream>
#include "../require.h"
#include "TStack2.h"
// Copyright notice in Copyright.txt
// (c) Bruce Eckel 2000
// Available at http://www.BruceEckel.com
// From Thinking in C++, 2nd Edition
//: C16:TStack2Test.cpp
Stack<string>::iterator it = textlines.begin();