[计算机科学经

5 c templates 1980 class templates function

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: = MAXSIZE) { throw std::out_of_range("Stack<>::push(): stack is full."); } elems[numElems] = elem; // 追加 ++numElems; // 元素總數加 1 } 使用㆖述 class template 時,必須同時指定 (1) 元素型別和 (2) stack 元素的最大數量: // basics/stack4test.cpp #include #include #include #include <iostream> <string> <cstdlib> "stack4.hpp" int main() { C++ Templates - The Complete Guide 4 38 try { int20Stack; Stack<int,20> int40Stack; Stack<int,40> Stack<std::string,40> stringStack; Nontype Template Parameters // 最多容納 20 個 int 元素 // 最多容納 40 個 int 元素 // 最多容納 40 個 string 元素 // 操控「最多容納 20 個 int 元素」的那個 stack int20Stack.push(7); std::cout << int20Stack.top() << std::endl; int20Stack.pop(); // 操控「最多容納 40 個 string 元素」的那個 stack stringStack.push("hello"); std::cout << stringStack.top() << std::end...
View Full Document

This note was uploaded on 08/25/2012 for the course EECS 101 taught by Professor Wre during the Fall '12 term at Beacon FL.

Ask a homework question - tutors are online