C ++ templates notes 5 class templates special

Source: Internet
Author: User

# Include <deque> <br/> # include <string> <br/> # include <stdexcept> <br/> // # include "stack1.hpp" <br/> # include <iostream> <br/> # include <string> <br/> # include <cstdlib> <br/> # include <vector> </P> <p> template <typename t> <br/> class Stack {<br/> PRIVATE: <br/> STD: vector <t> elems; // elements </P> <p> Public: <br/> void push (T const &); // push element <br/> void POP (); // pop element <br/> T top () c Onst; // return top element <br/> bool empty () const {// return whether the stack is empty <br/> return elems. empty (); <br/>}< br/>}; </P> <p> template <typename T> <br/> void stack <t> :: push (T const & ELEM) <br/>{< br/> elems. push_back (ELEM); // append copy of passed ELEM <br/>}</P> <p> template <typename T> <br/> void stack <t> :: pop () <br/>{< br/> If (elems. empty () {<br/> throw STD: out_of_range ("sta CK <>:: POP (): Empty stack "); <br/>}< br/> elems. pop_back (); // remove last element <br/>}</P> <p> template <typename T> <br/> T stack <t >:: top () const <br/> {<br/> If (elems. empty () {<br/> throw STD: out_of_range ("Stack <>:: top (): Empty stack "); <br/>}< br/> return elems. back (); // return copy of last element <br/>}</P> <p> template <> <br/> class Stack <STD :: string >{< br/> PRIVATE: <br/> STD: deque <STD:: String> elems; // elements </P> <p> Public: <br/> void push (STD: String const &); // push element <br/> void POP (); // pop element <br/> STD: String top () const; // return top element <br/> bool empty () const {// return whether the stack is empty <br/> return elems. empty (); <br/>}< br/>}; </P> <p> void stack <STD: String >:: push (STD :: string const & ELEM) <br/>{< br/> elems. push_back (ELEM); // append copy of PA Ssed ELEM <br/>}</P> <p> void stack <STD: String >:: POP () <br/>{< br/> If (elems. empty () {<br/> throw STD: out_of_range <br/> ("Stack <STD: String >:pop (): Empty stack "); <br/>}< br/> elems. pop_back (); // remove last element <br/>}</P> <p> STD: String Stack <STD: String >:: top () const <br/> {<br/> If (elems. empty () {<br/> throw STD: out_of_range <br/> ("Stack <STD: String >:: top (): Empty stack "); <br/>}< br/> Return elems. back (); // return copy of last element <br/>}</P> <p> int main () <br/>{< br/> try {<br/> stack <int> intstack; // stack of ints <br/> stack <STD: String> stringstack; // stack of strings </P> <p> // manipulate int stack <br/> intstack. push (7); <br/> STD: cout <intstack. top () <STD: Endl; <br/> intstack. pop (); </P> <p> // manipulate String Stack <br/> stringstack. push ("hello"); <br/> STD :: Cout <stringstack. top () <STD: Endl; <br/> stringstack. pop (); <br/> stringstack. pop (); <br/>}< br/> catch (STD: exception const & Ex) {<br/> STD: cerr <"exception: "<ex. what () <STD: Endl; <br/> return exit_failure; // exit program with error status <br/>}</P> <p>/* <br/> the class template is special, you can optimize the implementation of a specific type, or overcome the limitations of a specific type in the instantiation class template <br/>. If you want to create a class template, all member functions of the class template must be customized. <Br/> 7 <br/> Hello <br/> exception: Stack <STD: String >:: POP (): empty stack <br/> the execution result of the code above shows that it calls and executes the special function template <br/> */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.