• 模板的模板参数使用示例


    注意:函数模板不支持模板的模板参数。

    Stack7.h中定义的Stack:

    #ifndef STACK7_H
    #define STACK7_H
    
    #include <deque>
    #include <stdexcept>
    #include <memory>
    
    template <typename T,
            template <typename ELEM, typename = std::allocator<ELEM> >
            class CONT = std::deque>
    class Stack
    {
    public:
        void push(T const&);
        void pop();
        T top() const;
        bool empty() const
        {
            return elems.empty();
        }
    
        template<typename T2,
                template<typename ELEM2, typename = std::allocator<ELEM2> >
                class CONT2>
        Stack<T, CONT>& operator = (Stack<T2, CONT2> const&);
    private:
        CONT<T> elems;
    };
    
    template <typename T, template <typename, typename> class CONT>
    void Stack<T, CONT>::push(T const& elem)
    {
        elems.push_back(elem);
    }
    
    template <typename T, template <typename, typename> class CONT>
    void Stack<T, CONT>::pop()
    {
        if (elems.empty())
            throw std::out_of_range("Stack<>::pop(): empty stack");
    
        elems.pop_back();
    }
    
    template <typename T, template <typename, typename> class CONT>
    T Stack<T, CONT>::top() const
    {
        if (elems.empty())
            throw std::out_of_range("Stack<>::top(): empty stack");
    
        return elems.back();
    }
    
    template <typename T, template <typename, typename> class CONT>
    template <typename T2, template <typename, typename> class CONT2>
    Stack<T, CONT>& Stack<T, CONT>::operator = (Stack<T2, CONT2> const& op2)
    {
        if ((void*)this == (void*)&op2)
            return *this;
    
        Stack<T2, CONT2> tmp(op2);
        elems.clear();
        while (!tmp.empty())
        {
            elems.push_front(tmp.top());
            tmp.pop();
        }
    
        return *this;
    }
    
    #endif // STACK7_H

    测试代码main.cpp:

    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <vector>
    #include "Stack7.h"
    
    using namespace std;
    
    int main()
    {
        try
        {
            Stack<int, std::vector> vecStack;
            vecStack.push(2);
            vecStack.push(3);
            std::cout << "vector Stack's top: " << vecStack.top() << std::endl;
            vecStack.pop();
    
            Stack<int> intStack;
            Stack<float> floatStack;
    
            intStack.push(6);
            intStack.push(7);
    
            floatStack.push(9.9);
    
            floatStack = intStack;
    
            std::cout << floatStack.top() << std::endl;
            floatStack.pop();
            std::cout << "float Stack's top:" << floatStack.top() << std::endl;
            floatStack.pop();
            std::cout << "float Stack's top:" << floatStack.top() << std::endl;
            floatStack.pop();
        }
        catch (std::exception const& ex)
        {
            std::cerr << "Exception: " << ex.what() <<std::endl;
        }
    
        return 0;
    }

    结果:

  • 相关阅读:
    jQuery
    我的微信公众号被封了
    安装pillow
    win 10 无线标志都不出现
    msfvenom 摄像头
    Python list的定义和删改
    Python md5解密
    Python 同ip网站查询(制作网站接口)
    python 带参数运行
    Python 查看本机WiFi密码
  • 原文地址:https://www.cnblogs.com/AmitX-moten/p/4449135.html
Copyright © 2020-2023  润新知