1 该栈只用于存在int型数据
#include "../require.h"
#include <iostream> using namespace std; class IntStack
{
enum { ssize = };
int stack[ssize];
int top; public:
IntStack() : top() {}
void push(int i)
{
require(top < ssize, "Too many push()es");
stack[top++] = i; //int型元素,通过赋值运算符,直接存入IntStack栈内部的 int stack[]数组中
} int pop()
{
require(top > , "Too many pop()s");
return stack[--top];
} class iterator;
friend class iterator; class iterator
{
IntStack& intStack;
int index; public: iterator(IntStack& is) : intStack(is), index() {} iterator(IntStack& is, bool) : intStack(is), index(is.top) {} // 返回迭代器当前位置的元素
int current() const
{
return intStack.stack[index];
} // ++i
int operator++()
{
require(index < intStack.top, "iterator moved out of range");
return intStack.stack[++index];
} // i++
int operator++(int)
{
require(index < intStack.top, "iterator moved out of range");
return intStack.stack[index++];
} // i += 2;
iterator& operator+=(int amount)
{
require(index + amount < intStack.top, "IntStack::iterator::operator+=() tried to moved out of bounds");
index += amount;
return *this;
} bool operator==(const iterator& rv) const
{
return index == rv.index;
} bool operator!=(const iterator& rv) const
{
return index != rv.index;
} friend ostream& operator<<(ostream& os, const iterator& it);
}; iterator begin()
{
return iterator(*this);
} iterator end()
{
return iterator(*this, true);
}
}; // 重载 << 运算符 -- 全局的
ostream& operator<<(ostream& os, const IntStack::iterator& it)
{
return os << it.current();
} int main()
{
IntStack stack;
for (int i = ; i < ; i++)
{
stack.push(i*);
} //----------------------
cout << "Traverse the whole IntStack" << endl; IntStack::iterator it = stack.begin();
int n=;
while (it != stack.end())
{
cout << n++ << ": " << it++ << endl;
} //----------------------------------
cout << "Traverse a portion of the IntStack" << endl; IntStack::iterator start = stack.begin(), end2 = stack.end();
start += ;
end2 += ; //在此,程序退出 cout << "start = " << start << endl;
cout << "end = " << end2 << endl; while (start != end2)
{
cout << start++ << endl;
} return ;
};
运行结果:
----------------------------------------
附模板化实现(cp406):
容器中放的是值对象,通过重载后的=运算符完成对象数据成员的拷贝(只做了这一层,算是浅拷贝)
StackTemplate.h
#ifndef STACKTEMPLATE_H
#define STACKTEMPLATE_H template<class T>
class StackTemplate
{
enum { ssize = }; T stack[ssize];
int top; public:
StackTemplate() : top() {} void push(const T& i)
{
stack[top++] = i;
} T pop()
{
return stack[--top];
} int size()
{
return top;
}
};
#endif
测试文件
#include "fibonacci.h"
#include "StackTemplate.h"
#include <iostream>
#include <fstream>
#include <string>
#include "Book.h" using namespace std; int main()
{ cout << "---------- StackTemplate<int> ------------------" << endl;
StackTemplate<int> intTempl;
for (int i = ; i < ; i++)
{
intTempl.push(fibonacci(i));
}
for (int k = ; k < ; k++)
{
cout << intTempl.pop() << endl;
} cout << "---------- StackTemplate<string> ------------------" << endl; StackTemplate<string> stringTempl;
ifstream in("fibonacci.h");
string line; while (getline(in, line))
stringTempl.push(line); while (stringTempl.size() > )
cout << stringTempl.pop() << endl; cout << "---------- StackTemplate<Book> ------------------" << endl;
{
StackTemplate<Book> books; Book bk1("算法精解", "Kyle Loudon", 56.2);
Book bk2("Qt程序设计", "Pulat", 10.2); books.push(bk1);
books.push(bk2); } return ;
}
运行结果:
从运行结果可以看出,Book bk1("算法精解", "Kyle Loudon", 56.2);拷贝给了books.stack[0](地址,0018fc2c); Book bk2("Qt程序设计", "Pulat", 10.2);拷贝给了books.stack[1](地址,0018fc54)
从调用Book类析构函数的顺序上看,bk2 --> bk1 --> books(books.stack[9] --> books.stack[8] ...... --> books.stack[1] --> books.stack[0])
因为push参数是 const T& i使用的是引用(引用传递),所有元素入栈时没有产生新的Book对象。StackTemplate<Book>完全拥有Book对象,所以超出其作用域后,能够自动析构Book对象。