今天,看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[]{ ,,,,,, };
myVector<int> V{ arr,arr + };
myStack<int> S;
myStack<int>S2{ V }; cout << "test 1" << endl << endl;
cout << "栈1添加一个元素 10" << endl << endl;
S.push();
cout << "栈1添加一个元素 15" << endl << endl;
S.push();
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();
S.push(); 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 模拟实现的更多相关文章

  1. 洛谷 P1739 表达式括号匹配【STL/stack/模拟】

    题目描述 假设一个表达式有英文字母(小写).运算符(+,-,*,/)和左右小(圆)括号构成,以"@"作为表达式的结束符.请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返 ...

  2. <泛> STL - vector 模拟实现

    今天为大家带来一个模拟STL-vector的模板实现代码. 首先看一下测试结果,之后再为大家呈现设计 测试效果 测试代码 #include<iostream> #include<ve ...

  3. STL stack 容器

    STL stack 容器 Stack简介 stack是堆栈容器,是一种“先进后出”的容器.      stack是简单地装饰deque容器而成为另外的一种容器.      #include <s ...

  4. 浅谈C++ STL stack 容器

    浅谈C++ STL stack 容器 本篇随笔简单介绍一下\(C++STL\)中\(stack\)容器的使用方法和常见的使用技巧. stack容器的概念 \(stack\)在英文中是栈的意思.栈是一种 ...

  5. C++ 标准模板库(STL)-stack

    主要介绍一下C++11版本中标准模板库中栈的用法,希望可以帮到需要用的人. #include <iostream> #include <stack> #include < ...

  6. STL stack 常见用法详解

    <算法笔记>学习笔记 stack 常见用法详解 stack翻译为栈,是STL中实现的一个后进先出的容器.' 1.stack的定义 //要使用stack,应先添加头文件#include &l ...

  7. C++ STL stack和queue

    C++ STL中独立的序列式容器只有vector,list,deque三种,stack和queue其实就是使用容器适配器对deque进行了封装,使用了新接口. 使用标准库的栈和队列时,先包含相关的头文 ...

  8. C++ STL stack 用法

    Stack(栈)是一种后进先出的数据结构,也就是LIFO(last in first out) ,最后加入栈的元素将最先被取出来,在栈的同一端进行数据的插入与取出,这一段叫做“栈顶”. 使用STL的s ...

  9. STL——stack

    首先,堆栈是一个线性表,插入和删除只在表的一端进行.这一端称为栈顶(Stack Top),另一端则为栈底(Stack Bottom).堆栈的元素插入称为入栈,元素的删除称为出栈.由于元素的入栈和出栈总 ...

随机推荐

  1. python json 访问与字符串截取

    # req = requests.Request(url=url, headers=headers, data=data) # html = requests.get(req) # print(htm ...

  2. jQuery UI dialog 使用记录

    1 属性 1.11 autoOpen ,这个属性为true的时候dialog被调用的时候自动打开dialog窗口.当属性为false的时候,一开始隐藏窗口,知道.dialog("open&q ...

  3. 20145234黄斐《Java程序设计》第七周

    教材学习内容总结 第十二章部分 - Lambda 认识Lambda语法 Lambda去可以重复,符合DRY原则,而且Lambda表达式可读性更好,操作更简单 匿名类型最大的问题就在于其冗余的语法,la ...

  4. [转]softmax函数详解

    答案来自专栏:机器学习算法与自然语言处理 详解softmax函数以及相关求导过程 这几天学习了一下softmax激活函数,以及它的梯度求导过程,整理一下便于分享和交流. softmax函数 softm ...

  5. dropload的使用记录

    这次做一个H5的页面,需要用到上拉加载,下拉刷新的功能,在网上看到ximen写的dropload.js可以满足需求(此处致谢作者),但是用的时候还是踩了一些坑,这里记录下来备忘. 一些小问题:1. m ...

  6. mysql条件查询中AND与OR联合使用的注意事项!

    mysql查询中经常会用到AND与OR一起使用的情况,可如果写法不对,往往会起到相反的效果,这不,前几天就碰到了,最后测试果然提了一堆bug!!!! 废话就不多说了,主要总结一下几点: 一 当mysq ...

  7. bzoj 4816: 洛谷 P3704: [SDOI2017]数字表格

    洛谷很早以前就写过了,今天交到bzoj发现TLE了. 检查了一下发现自己复杂度是错的. 题目传送门:洛谷P3704. 题意简述: 求 \(\prod_{i=1}^{N}\prod_{j=1}^{M}F ...

  8. Method for balancing binary search trees

    Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...

  9. nand flash 的oob 及坏块管理

    0.NAND的操作管理方式      NAND FLASH的管理方式:以三星FLASH为例,一片Nand flash为一个设备(device),1 (Device) = xxxx (Blocks),1 ...

  10. 事件,使用.net自带委托EventHandler

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...