<泛> STL - stack 模拟实现
今天,看C++Template的时候看到那人写了一个Stack,于是乎,手痒,自己也写了一个,在拜读了STD文件和C++模板元编程某些小节之后,你们就看到了这篇代码。
经过上述一番经历之后,我重新写了myVector,使之更完善,更加服务于顶层结构,如:myStack
栈没什么写的,大部分精力都放在了重新构建底层容器上,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 模拟实现的更多相关文章
- 洛谷 P1739 表达式括号匹配【STL/stack/模拟】
题目描述 假设一个表达式有英文字母(小写).运算符(+,-,*,/)和左右小(圆)括号构成,以"@"作为表达式的结束符.请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返 ...
- <泛> STL - vector 模拟实现
今天为大家带来一个模拟STL-vector的模板实现代码. 首先看一下测试结果,之后再为大家呈现设计 测试效果 测试代码 #include<iostream> #include<ve ...
- STL stack 容器
STL stack 容器 Stack简介 stack是堆栈容器,是一种“先进后出”的容器. stack是简单地装饰deque容器而成为另外的一种容器. #include <s ...
- 浅谈C++ STL stack 容器
浅谈C++ STL stack 容器 本篇随笔简单介绍一下\(C++STL\)中\(stack\)容器的使用方法和常见的使用技巧. stack容器的概念 \(stack\)在英文中是栈的意思.栈是一种 ...
- C++ 标准模板库(STL)-stack
主要介绍一下C++11版本中标准模板库中栈的用法,希望可以帮到需要用的人. #include <iostream> #include <stack> #include < ...
- STL stack 常见用法详解
<算法笔记>学习笔记 stack 常见用法详解 stack翻译为栈,是STL中实现的一个后进先出的容器.' 1.stack的定义 //要使用stack,应先添加头文件#include &l ...
- C++ STL stack和queue
C++ STL中独立的序列式容器只有vector,list,deque三种,stack和queue其实就是使用容器适配器对deque进行了封装,使用了新接口. 使用标准库的栈和队列时,先包含相关的头文 ...
- C++ STL stack 用法
Stack(栈)是一种后进先出的数据结构,也就是LIFO(last in first out) ,最后加入栈的元素将最先被取出来,在栈的同一端进行数据的插入与取出,这一段叫做“栈顶”. 使用STL的s ...
- STL——stack
首先,堆栈是一个线性表,插入和删除只在表的一端进行.这一端称为栈顶(Stack Top),另一端则为栈底(Stack Bottom).堆栈的元素插入称为入栈,元素的删除称为出栈.由于元素的入栈和出栈总 ...
随机推荐
- [转载]ECMA-262 6th Edition / Draft August 24, 2014 Draft ECMAScript Language Specification
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-23.4 Draft Report Errors and Issues at: htt ...
- 博皮设计:HTML/CSS/Javascript 源码共享
首先感谢 sevennight 对我的大力帮助,由此他也成为了我的第一位园友:其次,由于本人并不了解 HTML/CSS,因此几乎都在 李宝亨 设计的 博皮源码 的基础上进行的修改:最后,为了获得 更加 ...
- 让PHPCms内容页支持JavaScript_
在PHPCms内容页中,出于完全考虑,默认是禁止JavaScript脚本的,所以我们在添加文章时,虽然加入了js代码,但实际上并没有起作用,而是以文本形式显示.如果要让内容页支持JavaScript, ...
- 2019年湖南多校第一场||2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)
第一场多校就打的这么惨,只能说自己太菜了,还需继续努力啊- 题目链接: GYM链接:https://codeforces.com/gym/101933 CSU链接:http://acm.csu.edu ...
- redis写定时任务获取root权限
前提: 1.redis由root用户启动. 2.开启cron的时候,/var/spool/cron linux机器下默认的计划任务,linux会定时去执行里面的任务. 启动服务 :/sbin/serv ...
- python内置模块之itertools
前言 itertools模块是python内置的迭代器模块,定义了可生成多种迭代器的函数,用来代替可迭代对象的遍历等操作,节约内存. 迭代器函数的类型 无限迭代器:包括count.cycle.repe ...
- sicily 1459. The Dragon of Loowater
Time Limit: 1sec Memory Limit:32MB Description Once upon a time, in the Kingdom of Loowa ...
- PowerDesigner导出word模版
模板下载 解压至:C:\Program Files (x86)\Sybase\PowerDesigner 15\Resource Files\Report Templates 即可 感谢http:// ...
- csslint在前端项目中的使用
大家都听说过jslint,eslint,不过你可能没见过csslint,你可能会问csslint有什么用,为什么今天要说csslint,是因为我在开发中遇到一个坑,其实之前不怎么使用csslint的, ...
- github后端开发面试题大集合(三)
作者:小海胆链接:https://www.nowcoder.com/discuss/3616来源:牛客网 13.软件架构相关问题: 什么情况下缓存是没用的,甚至是危险的? 为什么事件驱动的架构能提高可 ...