• c++ 模板类的 友元函数


      1 #pragma once
      2 #include <iostream>
      3 
      4 template <class T>
      5 class stack
      6 {
      7     template <class Ty>
      8     friend std::ostream& operator<<(std::ostream& os, const stack<Ty>& s);
      9 public:
     10     explicit stack<T>(int maxSize);
     11     stack<T>(const stack<T>& s);
     12     stack<T>(stack<T>&&) = delete;
     13     stack<T>& operator=(const stack& s);
     14     stack<T>& operator=(stack<T>&&) = delete;
     15     ~stack<T>();
     16 
     17     void push(T e);
     18     bool pop(T& e);
     19     bool getTop(T& e) const;
     20 
     21     bool isEmpty() const;
     22     int getNumElems() const;
     23 
     24 private:
     25     T* elems;
     26     int top;
     27     int maxSize;
     28     void overflowProcess();
     29 };
     30 
     31 template <class T>
     32 std::ostream& operator<<(std::ostream& os,const stack<T>& s)
     33 {
     34     for (int i = 0; i <= s.top; i++)
     35     {
     36         std::cout << s.elems[i] << " ";
     37     }
     38     return os;
     39 }
     40 
     41 template <class T>
     42 stack<T>::stack(int maxSize_): top(-1), maxSize(maxSize_)
     43 {
     44     this->elems = new T[this->maxSize];
     45 }
     46 
     47 template <class T>
     48 stack<T>::stack(const stack<T>& s)
     49 {
     50     this->top = s.top;
     51     this->maxSize = s.maxSize;
     52     this->elems = new T[this->maxSize];
     53     memcpy_s(this->elems, sizeof(T) * maxSize, s.elems, sizeof(T) * maxSize);
     54 }
     55 
     56 template <class T>
     57 stack<T>& stack<T>::operator=(const stack& s)
     58 {
     59     if (this == &s) return *this;
     60     if (this->elems != nullptr)delete[] elems;
     61     this->top = s.top;
     62     this->maxSize = s.maxSize;
     63     this->elems = new T[maxSize];
     64     memcpy_s(this->elems, sizeof(T) * maxSize, s.elems, sizeof(T) * maxSize);
     65     return *this;
     66 }
     67 
     68 template <class T>
     69 stack<T>::~stack()
     70 {
     71     if (this->elems != nullptr)
     72         delete[] elems;
     73 }
     74 
     75 template <class T>
     76 void stack<T>::push(T e)
     77 {
     78     if (this->top == maxSize - 1)
     79     {
     80         overflowProcess();
     81     }
     82     this->elems[++top] = e;
     83 }
     84 
     85 template <class T>
     86 bool stack<T>::pop(T& e)
     87 {
     88     if (!isEmpty())
     89     {
     90         e = this->elems[top--];
     91         return true;
     92     }
     93     return false;
     94 }
     95 
     96 template <class T>
     97 bool stack<T>::getTop(T& e) const
     98 {
     99     if (!isEmpty())
    100     {
    101         e = elems[top];
    102         return true;
    103     }
    104     return false;
    105 }
    106 
    107 template <class T>
    108 bool stack<T>::isEmpty() const
    109 {
    110     return top == -1 ? true : false;
    111 }
    112 
    113 template <class T>
    114 int stack<T>::getNumElems() const
    115 {
    116     return top + 1;
    117 }
    118 
    119 template <class T>
    120 void stack<T>::overflowProcess()
    121 {
    122     T* new_elems = new T[maxSize + maxSize / 3];
    123     memcpy_s(new_elems, sizeof(T) * maxSize, this->elems, sizeof(T) * maxSize);
    124     delete[] elems;
    125     this->elems = new_elems;
    126 }

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

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

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

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

              

  • 相关阅读:
    互联网公司笔试常见陷阱
    华为机试题(地铁换乘,图的应用)
    容器(vector)、数组、new创建的动态数组,你到底用哪一个(执行效率分析)
    SpringMVC全局异常
    github commit contributions无记录
    elasticSearch之查询
    nginx负载均衡策略
    docker入门
    解决centos&win安装安装elasticSearch无法运行
    2017年终总结
  • 原文地址:https://www.cnblogs.com/infoo/p/7684209.html
Copyright © 2020-2023  润新知