#pragma once
#include <iostream> template <class T>
class stack
{
template <class Ty>
friend std::ostream& operator<<(std::ostream& os, const stack<Ty>& s);
public:
explicit stack<T>(int maxSize);
stack<T>(const stack<T>& s);
stack<T>(stack<T>&&) = delete;
stack<T>& operator=(const stack& s);
stack<T>& operator=(stack<T>&&) = delete;
~stack<T>(); void push(T e);
bool pop(T& e);
bool getTop(T& e) const; bool isEmpty() const;
int getNumElems() const; private:
T* elems;
int top;
int maxSize;
void overflowProcess();
}; template <class T>
std::ostream& operator<<(std::ostream& os,const stack<T>& s)
{
for (int i = ; i <= s.top; i++)
{
std::cout << s.elems[i] << " ";
}
return os;
} template <class T>
stack<T>::stack(int maxSize_): top(-), maxSize(maxSize_)
{
this->elems = new T[this->maxSize];
} template <class T>
stack<T>::stack(const stack<T>& s)
{
this->top = s.top;
this->maxSize = s.maxSize;
this->elems = new T[this->maxSize];
memcpy_s(this->elems, sizeof(T) * maxSize, s.elems, sizeof(T) * maxSize);
} template <class T>
stack<T>& stack<T>::operator=(const stack& s)
{
if (this == &s) return *this;
if (this->elems != nullptr)delete[] elems;
this->top = s.top;
this->maxSize = s.maxSize;
this->elems = new T[maxSize];
memcpy_s(this->elems, sizeof(T) * maxSize, s.elems, sizeof(T) * maxSize);
return *this;
} template <class T>
stack<T>::~stack()
{
if (this->elems != nullptr)
delete[] elems;
} template <class T>
void stack<T>::push(T e)
{
if (this->top == maxSize - )
{
overflowProcess();
}
this->elems[++top] = e;
} template <class T>
bool stack<T>::pop(T& e)
{
if (!isEmpty())
{
e = this->elems[top--];
return true;
}
return false;
} template <class T>
bool stack<T>::getTop(T& e) const
{
if (!isEmpty())
{
e = elems[top];
return true;
}
return false;
} template <class T>
bool stack<T>::isEmpty() const
{
return top == - ? true : false;
} template <class T>
int stack<T>::getNumElems() const
{
return top + ;
} template <class T>
void stack<T>::overflowProcess()
{
T* new_elems = new T[maxSize + maxSize / ];
memcpy_s(new_elems, sizeof(T) * maxSize, this->elems, sizeof(T) * maxSize);
delete[] elems;
this->elems = new_elems;
}

代码长;懒得剪。。。。一个具备基本功能的栈类;可以直接使用

里面用到了:  模板友元函数   在类外定义的前面要加上template<class Ty> 以示区分

因此:  模板友元函数:类内定义       无需 template<class Ty>  /////其实这个还不确定,下次试试就知道了

类内声明,类外定义:需要tempalte<class Ty>

c++ 模板类的 友元函数的更多相关文章

  1. c/c++ 模板与STL小例子系列<二> 模板类与友元函数

    c/c++ 模板与STL小例子系列 模板类与友元函数 比如某个类是个模板类D,有个需求是需要重载D的operator<<函数,这时就需要用到友元. 实现这样的友元需要3个必要步骤 1,在模 ...

  2. C++模板类中友元函数的写法

    首先,已声明好的类Triangle file://Triangle.h template<class T> class Triangle{ public: Triangle(T width ...

  3. gcc的bug? c++模板类中友元函数的訪问权限问题

    原文地址:http://stackoverflow.com/q/23171337/3309790 在c++中,模板类中能够直接定义一个友元函数.该函数拥有訪问该模板类非public成员的权限. 比方: ...

  4. C++中模板类使用友元模板函数

    在类模板中可以出现三种友元声明:(1)普通非模板类或函数的友元声明,将友元关系授予明确指定的类或函数.(2)类模板或函数模板的友元声明,授予对友元所有实例的访问权.(3)只授予对类模板或函数模板的特定 ...

  5. C++模板类内友元(友元函数,友元类)声明的三种情况

    根据<C++ Primer>第三版16.4节的叙述,C++类模板友元分为以下几种情况 1.非模板友元类或友元函数. 书上给了一个例子: class Foo{     void bar(); ...

  6. C++学习之友元类和友元函数

    C++学习之友元类和友元函数       模板类声明也可以有友元,模板的友元可以分为以下几类:        1.非模板友元:        2.约束模板友元,即就是友元的类型取决于类被实例化的时候的 ...

  7. sdut 3-7 类的友元函数的应用

    3-7 类的友元函数的应用 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 通过本题目的练习能够掌握类的友元函数的定义和使用方法 要求设计一个点类Poin ...

  8. C++的友元类和友元函数实例

    #include <math.h> #include<iostream> using namespace std; class Point { public: Point(do ...

  9. mfc 类的友元函数

    知识点 友元函数 友元函数 友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数..类授予它的友元特别的访问权.通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你 ...

随机推荐

  1. Github和Azure DevOps的代码同步

    [前言]Github和Azure DevOps都提供了Git代码库功能,那么有没有办法将两边的代码库进行同步呢,答案是肯定的.这里的操作我都是用Azure DevOps的Pipelines功能来完成的 ...

  2. web网页动态分享facebook和twitter

    介绍 facebook分享 http://www.facebook.com/sharer.php?t=${text}u=encodeURIComponent('静态html') twitter分享 h ...

  3. 实现 (5).add(3).minus(2) 功能

    Number.prototype.add = function (number) { if (typeof number !== 'number') { throw new Error('请输入数字- ...

  4. 01-MySQL支持的数据类型

    1.数值类型 整数类型 MySQL 支持的整数类型有 SQL 标准中的整数类型 INTEGER,SMALLINT,TINYINT.MEDIUMINT和BIGINT.其整数类型的特性如下表所示: 在上述 ...

  5. 09-Python异常

    一.简介 在实际的工作过程中,我们会遇到各种问题,比如文件不存在,代码运行不符合某些特定逻辑等,程序在运行时,遇到这些问题便会发生异常.英文是Exception. a = float(input('请 ...

  6. bzoj4512[Usaco2016 Jan] Build Gates

    bzoj4512[Usaco2016 Jan] Build Gates 题意: 某人从农场的(0,0)出发,沿边界到处乱走,走过的地方会留下栅栏,等走完后问要在多少个栅栏上开门才能使整个农场连通,最多 ...

  7. Docker 基础知识 - 使用卷(volume)管理应用程序数据

    卷(volumes)是 Docker 容器生产和使用持久化数据的首选机制.绑定挂载(bind mounts)依赖于主机的目录结构,卷(volumes)完全由 Docker 管理.卷与绑定挂载相比有几个 ...

  8. Oracle版本发布规划 (文档 ID 742060.1)

    Oracle Database Release Schedule of Current Database Releases (文档 ID 742060.1) Oracle Database RoadM ...

  9. “git pull” 强制覆盖本地文件

    放弃本地修改,使用服务器代码覆盖本地的Git命令如下: $ git fetch --all $ git reset --hard origin/master $ git pull 使用master分支 ...

  10. Monster Audio 使用教程(一)入门教程 + 常见问题

    Monster Audio支持的操作系统: windows 7 64bit 至 windows 10 64bit 受支持的VST: Vst 64bit.Vst3 64bit 受支持的声卡驱动: ASI ...