Templated class features

Source: Internet
Author: User
  1. // Stack. cpp: defines the entry point for the console application.
  2. //
  3. # Include "stdafx. H"
  4. # Include <iostream>
  5. # Include <string>
  6. # Include <vector>
  7. # Include <deque>
  8. # Include <stdexcept>
  9. Using namespace STD;
  10. Template <class T>
  11. Class Stack
  12. {
  13. Public:
  14. Void push (T const &);
  15. Void POP ();
  16. T top () const;
  17. Bool empty () const;
  18. PRIVATE:
  19. Deque <t> m_vtelem;
  20. };
  21. Template <class T>
  22. Void stack <t>: Push (T const & ELEM)
  23. {
  24. M_vtelem.push_back (ELEM );
  25. }
  26. Template <class T>
  27. Void stack <t >:: POP ()
  28. {
  29. // Judge whether the stack is empty
  30. If (m_vtelem.empty ())
  31. {
  32. Throw out_of_range ("Stack <>: Pop empty stack ");
  33. }
  34. // The last element is displayed.
  35. M_vtelem.pop_back ();
  36. }
  37. Template <typename T>
  38. T stack <t>: Top () const
  39. {
  40. If (m_vtelem.empty ())
  41. {
  42. Throw out_of_range ("Stack <>: Top empty stack! ");
  43. }
  44. Return m_vtelem.back ();
  45. }
  46. Template <typename T>
  47. Bool stack <t >:: empty () const
  48. {
  49. Return m_vtelem.empty ();
  50. }
  51. Template <>
  52. Class Stack <string>
  53. {
  54. Public:
  55. Void push (string const &);
  56. Void POP ();
  57. String top () const;
  58. Bool empty () const;
  59. PRIVATE:
  60. Deque <string> m_vtelem;
  61. };
  62. Void stack <string>: Push (string const & ELEM)
  63. {
  64. M_vtelem.push_back (ELEM );
  65. }
  66. Void stack <string >:: POP ()
  67. {
  68. If (m_vtelem.empty ())
  69. {
  70. Throw out_of_range ("Stack <string>: Pop empty stack ");
  71. }
  72. M_vtelem.pop_back ();
  73. }
  74. String Stack <string >:: top () const
  75. {
  76. If (m_vtelem.empty ())
  77. {
  78. Throw out_of_range ("Stack <string >:: top () Empty stack! ");
  79. }
  80. Return m_vtelem.back ();
  81. }
  82. Bool stack <string >:: empty () const
  83. {
  84. Return m_vtelem.empty ();
  85. }
  86. Int main ()
  87. {
  88. Try
  89. {
  90. Stack <int> intstack;
  91. Stack <string> strstack;
  92. Intstack. Push (7 );
  93. Cout <intstack. Top () <Endl;
  94. Strstack. Push ("hello ");
  95. Cout <strstack. Top () <Endl;
  96. Intstack. Pop ();
  97. Strstack. Pop ();
  98. }
  99. Catch (exception const & exp)
  100. {
  101. Cerr <"exception:" <exp. What () <Endl;
  102. Return exit_failure;
  103. }
  104. Return 0;
  105. }

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.