待解決的問題:ios
1.未實現迭代器ide
2.頭文件和源文件內容只能放在一個文件裏面編譯,不然會出現"LNK2019 沒法解析的外部符號 ... ..."。spa
代碼:ci
#include <vector> #include <iostream> #include <fstream> #include <string> using namespace std; template <class Item> class ResizingArrayStack { private: vector<Item> a; int N; void resize(int max); public: ResizingArrayStack(int cap); ~ResizingArrayStack(); bool isEmpty(); int size(); void push(Item item); Item pop(); //Item next(); void test(); }; template <class Item> ResizingArrayStack<Item>::ResizingArrayStack(int cap) { a.resize(cap); N = 0; }; template <class Item> ResizingArrayStack<Item>::~ResizingArrayStack() {}; template <class Item> bool ResizingArrayStack<Item>::isEmpty() { return N == 0; } template <class Item> int ResizingArrayStack<Item>::size() { return N; } template <class Item> void ResizingArrayStack<Item>::resize(int max) { vector<Item> temp(max); for (int i = 0; i < N; i++) { temp[i] = a[i]; } a = temp; temp.clear(); } template <class Item> void ResizingArrayStack<Item>::push(Item item) { if (N == a.size()) resize(2 * a.size()); a[N++] = item; } template <class Item> Item ResizingArrayStack<Item>::pop() { Item item = a[--N]; if (N > 0 && N == a.size() / 4) resize(a.size() / 2); return item; } template <class Item> void ResizingArrayStack<Item>::test() { ResizingArrayStack<string> s(1); string file; ifstream tobe; cout << "輸入文件名:"; cin >> file; tobe.open(file, ios::in); while (!tobe.eof()) { string item; tobe >> item; if (!item._Equal("-")) s.push(item); else if (!s.isEmpty()) cout << s.pop() << " "; } cout << "(" << s.size() << " left on stack)"; }