- 用模板函数实现stack
- #include <iostream>
- using namespace std;
- template<typename T>
- class Seqlist
- {
- private:
- T *_data;
- int _size;
- int _capacity;
- public:
- Seqlist()
- :_size(0), _capacity(3), _data(0)
- {
- _data = new T[_capacity];
- }
- ~Seqlist()
- {
- if (_data != NULL)
- delete[] _data;
- _data = NULL;
- }
- void Checkcapacity()
- {
- if (_size == _capacity)
- {
- T *tmp = new T[2 * _capacity + 3];
- memcpy(tmp, _data, _size*sizeof(T));
- delete _data;
- _data = NULL;
- _data = tmp;
- _capacity = 2 * _capacity + 3;
- }
- }
- Seqlist(const Seqlist&s)
- {
- _data = new T[s._capacity];
- _size = s._size;
- _capacity = s._capacity;
- memcpy(_data, s._data, _capacity*sizeof(T));
- }
- Seqlist &operator=(const Seqlist&s)
- {
- if (this != &s)
- {
- delete[]_data;
- _data = new T[s._capacity];
- _size = s._size;
- _capacity = s._capacity;
- memcpy(_data, s._data, _capacity*sizeof(T));
- }
- return *this;
- }
- void PushBack(const T&d)
- {
- Checkcapacity();
- *(_data + _size) = d;
- _size++;
- }
- void PopBack()
- {
- _size--;
- }
- void PushFront(const T&d)
- {
- Checkcapacity();
- int i = 0;
- for (i = _size; i > 0; i--)
- {
- *(_data + i) = *(_data + i - 1);
- }
- *_data = d;
- _size++;
- }
- void PopFront()
- {
- int i = 0;
- for (i = 0; i < _size - 1; i++)
- *(_data + i) = *(_data + i + 1);
- _size--;
- }
- void Insert(int pos, const T&d);
- void Sort()
- {
- T tmp = 0;
- int i = 0;
- int j = 0;
- for (i = 0; i < _size - 1; i++)
- for (j = i + 1; j < _size; j++)
- {
- if (*(_data + i) >*(_data + j))
- {
- tmp = *(_data + i);
- *(_data + i) = *(_data + j);
- *(_data + j) = tmp;
- }
- }
- }
- void print()
- {
- int i = 0;
- for (i = 0; i < _size; i++)
- cout << *(_data + i) << " ";
- }
- };
- template<typename T>
- void Seqlist<T>::Insert(int pos, const T&d)
- {
- Checkcapacity();
- int i = 0;
- for (i = _size; i>pos; i--)
- *(_data + i) = *(_data + i - 1);
- *(_data + pos) = d;
- _size++;
- }
- template <typename T,typename Container=Seqlist <T> >
- class Stack
- {
- private:
- Container con;
- public:
- void Push(const T&d)
- {
- con.PushBack(d);
- }
- void Pop()
- {
- con.PopBack();
- }
- void print()
- {
- con.print();
- }
- };
- int main()
- {
- Stack<int, Seqlist<int> >stack1;
- stack1.Push(1);
- stack1.Push(2);
- stack1.Push(3);
- stack1.Push(4);
- stack1.print();
- getchar();
- return 0;
- }
- 2.运用stack
- #include <iostream>
- #include <stack>
- using namespace std;
- int main()
- {
- stack<int>s;
- s.push(1);
- s.push(2);
- s.push(3);
- return 0;
- }
- 3.模板的模板参数
- template<typename T,template<class> class Container=Seqlist >
- class Stack
- {
- private:
- Container<T> con;
- public:
- void Push(const T&d)
- {
- con.PushBack(d);
- }
- void Pop()
- {
- con.PopBack();
- }
- void print()
- {
- con.print();
- }
- };
- int main()
- {
- Stack<int>s;
- s.Push(1);
- s.Push(2);
- s.Push(3);
- s.print();
- getchar();
- return 0;
- }
- 4.非类型的类模板参数
- template <typename T,int MAX_SIZE=10>
- class Seqlist
- {
- private:
- T data[MAX_SIZE];
- //public:
- // void show( int a)
- // {
- // cout << "show()" << endl;
- // }
- };
- //template <typename T, int MAX_SIZE = 10>
- //void Seqlist<T, MAX_SIZE>::show(int a)
- //{
- // cout << "show()" << endl;
- //}
- int main()
- {
- Seqlist<int, 10>s1;
- Seqlist <int, 20>s2;
- getchar();
- return 0;
- }
- 5.非类型的模板函数参数
- template <typename T,int val>
- T add(const T& x)
- {
- return x + val;
- }
- int main()
- {
- int ret = add<int, 3>(1);
- cout << ret << endl;
- getchar();
- return 0;
- }
时间: 2024-10-06 08:07:17