1. 程式人生 > ><泛> STL - stack 模擬實現

<泛> STL - stack 模擬實現

自己 rhs bubuko under typename option tac png 你們

今天,看C++Template的時候看到那人寫了一個Stack,於是乎,手癢,自己也寫了一個,在拜讀了STD文件和C++模板元編程某些小節之後,你們就看到了這篇代碼。

經過上述一番經歷之後,我重新寫了myVector,使之更完善,更加服務於頂層結構,如:myStack

myVector實現

棧沒什麽寫的,大部分精力都放在了重新構建底層容器上,STL裏面的功能函數基本都實現了,除了std的各種相關的構造函數實在整不來那麽多

測試效果:

#include "E:\數據結構\myStack.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) { int arr[7]{ 1,3,5,8,7,4,55 }; myVector<int> V{ arr,arr + 7 }; myStack<int> S; myStack<int>S2{ V }; cout << "test 1" << endl << endl; cout << "棧1添加一個元素 10" << endl << endl; S.push(10); cout
<< "棧1添加一個元素 15" << endl << endl; S.push(15); cout << "棧1頂部元素為:"; cout << S.top() << endl << endl; S.pop(); cout << "彈出棧1頂部元素" << endl << endl; cout << "棧1頂部元素為:"; cout << S.top() << endl << endl; S.pop(); cout
<< "彈出棧1頂部元素" << endl << endl; cout << "棧1添加元素 10、15" << endl << endl; S.push(5); S.push(12); cout << "交換棧1棧2" << endl << endl; S.swap(S2); cout << "test 2" << endl << endl; cout << "棧1:" << endl; myStack<int>::container_type container = S._Get_container(); for (auto it : container) cout << it << " "; cout << endl << endl; cout << "棧2:" << endl; myStack<int>::container_type container2 = S2._Get_container(); for (auto it : container2) cout << it << " "; cout << endl; }

技術分享圖片

Template代碼:

#ifndef _MY_STACK
#define _MY_STACK

#include <E:\數據結構\myVector.h>
template<class T, 
    class myContainer = myVector<T> >
    class myStack
    {
public:  //public date-type information used by class design
typedef myStack
<T, myContainer> _Mytype; typedef myContainer container_type; typedef typename myContainer::value_type value_type; typedef typename myContainer::size_type size_type; typedef typename myContainer::_pointer _pointer; #define self (*this) public: //basic functions of class myStack() { elems.clear(); } myStack(const _Mytype& rhs) :elems(rhs.elems) { } explicit myStack(const myContainer& _container) :elems(_container) { } _Mytype& operator=(const _Mytype& other) { elems = other.elems; return self; } public: //some operator-loading functions of stack bool operator==(const _Mytype& rhs) { return elems == rhs.elems; } bool operator!=(const _Mytype& rhs) { return elems != rhs.elems; } bool operator<(const _Mytype& rhs) { return elems < rhs.elems; } bool operator>(const _Mytype& rhs) { return elems > rhs.elems; } bool operator<=(const _Mytype& rhs) { return !(self > rhs); } bool operator>=(const _Mytype& rhs) { return !(self < rhs); } public: // main options of stack void push(T const& item) { //尾部添加元素 elems.push_back(item); } void pop() { //將頂部元素刪除 if (elems.empty()) throw "myStack<>::pop(): empty stack\n"; elems.pop_back(); } const T& top()const { //取頂部元素 if (elems.empty()) throw "myStack<>::top(): empty stack\n"; return elems.back(); } bool empty()const { //判空 return elems.empty(); } void swap(_Mytype& rhs) { //交換數據 elems.swap(rhs.elems); } size_type size()const { //get the size of stack return elems.size(); } const myContainer& _Get_container()const { //Get the container of stack return elems; } private: myContainer elems; }; #endif

感謝您的閱讀,生活愉快~

<泛> STL - stack 模擬實現