body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

栈:(stack.h)

#ifndef __STACK_H__
#define __STACK_H__
#include <iostream>
#include<string>
#include<sstream>
using namespace std;
template <typename T,int num>
class Stack
{
        private:
                int _top;
                T _parr[num];
        public:
                Stack();
                ~Stack();
                bool full();
                bool empty();
                bool push(T elem);
                bool pop(T &);
                int getPos()
                {
                        return _top;
                }
};
template <typename T,int num>
Stack<T,num>::Stack():_top(-1)
{}
template <typename T,int num>
Stack<T,num>::~Stack()
{}
template <typename T,int num>
bool Stack<T,num>::full()
{
        return _top == (num-1);
}
template <typename T,int num>
bool Stack<T,num>::empty()
{
        return _top == -1;
}
template <typename T,int num>
bool Stack<T,num>::push(T elem)
{
        if(!full())
        {
                _parr[++_top] = elem;
                return true;
        }
        return false;
}
template <typename T,int num>
bool Stack<T,num>::pop(T & t)
{
        if(!empty())
        {
                t = _parr[_top--];
                return true;
        }
        else
                return false;
}
#endif
测试代码(testStack.cpp)


#include"stack.h"
int test0(void)
{      
        Stack<int, 10> stackInt;
        cout << "开始时stakcInt是否为空?" << stackInt.empty() << endl;
        stackInt.push(5);
        cout << "此始时stakcInt是否为空?" << stackInt.empty() << endl;
        for(int idx = 1; idx !=10; ++idx)
        {
                stackInt.push(idx);
        }
        cout << "此时stakcInt是否已满?" << stackInt.full() << endl;
        for(int idx = 0; idx != 10; ++idx)
        {
                int elem = 0;
                stackInt.pop(elem);
                cout << elem << " ";
        }
        cout << endl;
        return 0;
}
int test1(void)
{      
        Stack<string, 10> stackInt;
        cout << "开始时stakcInt是否为空?" << stackInt.empty() << endl;
        stackInt.push("aa");
        cout << "此始时stakcInt是否为空?" << stackInt.empty() << endl;
        for(int idx = 1; idx !=10; ++idx)
        {
                string s(2, 'a' + idx);  
//string类的一个构造函数,表示含有2个元素的string对象,其中每个元素都初始化为后面的字符
                stackInt.push(s);
        }
        cout << "此时stakcInt是否已满?" << stackInt.full() << endl;
        for(int idx = 0; idx != 10; ++idx)
        {
                string elem;
                stackInt.pop(elem);
                cout << elem << " ";
        }
        cout << endl;
        return 0;
}
int main()
{
        test0();
        test1();
        return 0;
}

栈(stack),C++模板实现的更多相关文章

  1. C++:栈(stack)的模板类实现

    1.基本概念 栈中的元素遵守“先进后出”的原则(LIFO,Last In First Out) 只能在栈顶进行插入和删除操作 压栈(或推入.进栈)即push,将数据放入栈顶并将栈顶指针加一 出栈(或弹 ...

  2. STL(标准模板库) 中栈(stack)的使用方法

    STL 中栈的使用方法(stack) 基本操作: stack.push(x)  将x加入栈stack中,即入栈操作 stack.pop()  出栈操作(删除栈顶),只是出栈,没有返回值 stack.t ...

  3. C++ Templates (2.2 使用Stack类模板 Use of Class Template Stack )

    返回完整目录 目录 2.2 使用Stack类模板 Use of Class Template Stack 2.2 使用Stack类模板 Use of Class Template Stack 在C++ ...

  4. BSS段 data段 text段 堆heap 和 栈stack

    BSS段:BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属于静态内存分配.   数 ...

  5. [转]JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )

    这两天看了一下深入浅出JVM这本书,推荐给高级的java程序员去看,对你了解JAVA的底层和运行机制有比较大的帮助.废话不想讲了.入主题: 先了解具体的概念:JAVA的JVM的内存可分为3个区:堆(h ...

  6. 堆heap和栈Stack(百科)

    堆heap和栈Stack 在计算机领域,堆栈是一个不容忽视的概念,堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈 ...

  7. (转)Java里的堆(heap)栈(stack)和方法区(method)(精华帖,多读读)

    [color=red][/color]<一> 基础数据类型直接在栈空间分配, 方法的形式参数,直接在栈空间分配,当方法调用完成后从栈空间回收.   引用数据类型,需要用new来创建,既在栈 ...

  8. 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现

      本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型   栈是 ...

  9. 栈stack(2):栈的链表实现

    定义 从上一篇我们知道,栈(stack)是一个只允许一端进行删除插入操作的线性表.同时,我们联想到线性表的链式结构,其特点是用一组任意的存储单元存储线性表的数据元素,因此我们选择使用链表去实现栈,规定 ...

随机推荐

  1. 一个改写MBR的例子

    前言 想要对MBR类的病毒进行一下研究与学习,在此期间,看了很多资料,其中帮助最大的就是金龟子学姐和willj学长发表的文章.一个从源码与实现角度来讲了一下,另外一个从反病毒角度来分析.   功能描述 ...

  2. 【第三十七章】 springboot+docker(手动部署)

    一.下载centos镜像 docker pull hub.c.163.com/library/centos:latest docker tag containId centos:7 docker ru ...

  3. 51nod 1106 质数检测

    #include <bits/stdc++.h> using namespace std; int n; ; bool s[maxn]; void is_prime() { memset( ...

  4. 【Python】【内置函数】【bytes&bytearray&str&array】

    [bytes] 英文文档: class bytes([source[, encoding[, errors]]]) Return a new “bytes” object, which is an i ...

  5. shell printf

    printf 可以格式化字符串,还可以制定字符串的宽度.左右对齐方式等.默认 printf 不会像 echo 自动添加换行符,我们可以手动添加 \n. 例子: $ echo "Hello, ...

  6. Selenium UI自动化测试 Selenium Automatic Testing

    https://www.cnblogs.com/sunada2005/archive/2013/12/22/3486314.html UI Automatic Testing 1. 什么样的项目适合自 ...

  7. unity项目针对IOS及Android平台的音频压缩格式

    IOS : 建议采用MP3格式, Android : 建议采用Vorbis格式, 因为这两种格式分别在这两个平台上有硬件解码的支持, 硬件解码比软件解码快.

  8. C++ 利用栈解决运算问题

    2017-06-27 19:19:18 第一步需要将中缀表达式转为后缀表达式.这步的转化可以说是本题的核心. 主要的转化手段是利用栈,有如下几个规则: 数字直接输出 "("直接进栈 ...

  9. spring boot: thymeleaf模板引擎使用

    spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...

  10. Dajngo的CBV和FBV

    CBV: class. base. view 路由: url(r'students/', views.StudentsView.as_view()) 视图: from django.views imp ...