zoukankan      html  css  js  c++  java
  • c++ template笔记(3)非类型模板参数nontype template parameters

    算是模板使用的一种技巧

    用模板当数组元素参数维度

    #include <stdexcept>
    
    template <typename T, int MAXSIZE>
    class Stack {
      private:
        T elems[MAXSIZE];        // elements
        int numElems;            // current number of elements
    
      public:
        Stack();                  // constructor
        void push(T const&);      // push element
        void pop();               // pop element
        T top() const;            // return top element
        bool empty() const {      // return whether the stack is empty
            return numElems == 0;
        }
        bool full() const {       // return whether the stack is full
            return numElems == MAXSIZE;
        }
    };
    
    // constructor
    template <typename T, int MAXSIZE>
    Stack<T,MAXSIZE>::Stack ()
      : numElems(0)               // start with no elements
    {
        // nothing else to do
    }
    
    template <typename T, int MAXSIZE>
    void Stack<T,MAXSIZE>::push (T const& elem)
    {
        if (numElems == MAXSIZE) {
            throw std::out_of_range("Stack<>::push(): stack is full");
        }
        elems[numElems] = elem;   // append element
        ++numElems;               // increment number of elements
    }
    
    template<typename T, int MAXSIZE>
    void Stack<T,MAXSIZE>::pop ()
    {
        if (numElems <= 0) {
            throw std::out_of_range("Stack<>::pop(): empty stack");
        }
        --numElems;               // decrement number of elements
    }
    
    template <typename T, int MAXSIZE>
    T Stack<T,MAXSIZE>::top () const
    {
        if (numElems <= 0) {
            throw std::out_of_range("Stack<>::top(): empty stack");
        }
        return elems[numElems-1];  // return last element
    }
    
    

    使用时就可以根据需要指定最大的元素个数,不用造成资源的浪费

    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include "stack4.hpp"
    
    int main()
    {
        try {
            Stack<int,20>         int20Stack;     // stack of up to 20 ints
            Stack<int,40>         int40Stack;     // stack of up to 40 ints
            Stack<std::string,40> stringStack;    // stack of up to 40 strings
    
            // manipulate stack of up to 20 ints
            int20Stack.push(7);
            std::cout << int20Stack.top() << std::endl;
            int20Stack.pop();
    
            // manipulate stack of up to 40 strings
            stringStack.push("hello");
            std::cout << stringStack.top() << std::endl; 
            stringStack.pop();
            stringStack.pop();
        }
        catch (std::exception const& ex) {
            std::cerr << "Exception: " << ex.what() << std::endl;
            return EXIT_FAILURE;  // exit program with ERROR status
        }
    }
    
    

    注意:被实例化出来的int20Stack和int40Stack不可以隐式转换.

    模板成员参数

    template <typename T, int VAL>
    T addValue (T const& x)
    {
        return x + VAL;
    }
    

    特化的参数,用于不在于类型,直接适用于函数中

    注意点:浮点类型不可以作为nontype template parameters

    以下定义是错误的

    //error
    template <double T>
    double process(double v)
    {
        return v*T;
    }
    
  • 相关阅读:
    初识面向对象
    Git 子模块
    至今为止项目中遇到的问题
    vuex , 简单入(liao)门(jie)
    Git rebase
    Git
    js设计模式工厂模式
    vue slot
    Jquery学习,一道笔试题:关于表格【最近记】
    闭包与this学习
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1977262.html
Copyright © 2011-2022 走看看