c++ template笔记(3)非类型模板参数nontype template parameters
来源:互联网 发布:如何卸载mac的app 编辑:程序博客网 时间:2024/05/16 00:52
算是模板使用的一种技巧
用模板当数组元素参数维度
#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; }};// constructortemplate <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
以下定义是错误的
//errortemplate <double T>double process(double v){ return v*T;}
0 0
- c++ template笔记(3)非类型模板参数nontype template parameters
- Nontype Class Template Parameters(非类型类模板参数)
- C++ - 非类型模板参数(nontype template parameters) 使用 及 代码
- C++ - 非类型模板参数(nontype template parameters) 使用 及 代码
- Nontype template parameters(非型别模板参数)
- Template Template Parameters(双重模板参数)
- template non-type parameter 非类型参数
- C++Template学习笔记之函数模板
- Template template parameter(模板参数) example
- C++template ;模板学习
- C++template模板
- [笔记]python template 模板
- 模板template课后笔记
- template template parameter模板模板参数笔记----C++学习之路
- c++无类型参数模板(non-type template parameter)
- [c++ template]非类型的函数模板以及非类型的类的模板
- 模板 template 的类型转换
- 练习笔记 C++Template
- 【C#、SQL】学习与错误记录(一)
- HDOJ-最大子矩阵(二维dp)
- imregionalmax
- c++ template(4)基本技巧
- 蓝桥杯+进制转换+STL stack的使用
- c++ template笔记(3)非类型模板参数nontype template parameters
- win7+Cygwin+Eclipse搭建Hadoop开发环境
- jenkins配置邮件报错:501 mail from address must be same as authorization user
- 进程线程及堆栈关系的总结
- 通过GET方式传递 数据到服务器
- c++ template笔记(2)模板类
- 深度学习介绍(三)LeNet
- 【进攻Android】Android、Bootstrap、Python
- Codeforces Round #177 (Div. 2)---D. Polo the Penguin and Houses (组合数学+暴力)