• C++ template class


    Code

    1. //stack.h
    2. #pragma once
    3. #include
    4. template <class T>
    5. class Stack
    6. {
    7. public:
    8. Stack(int = 10) ;
    9. ~Stack() { delete [] stackPtr ; }
    10. int push(const T&);
    11. int pop(T&) ; // pop an element off the stack
    12. int isEmpty()const { return top == -1 ; }
    13. int isFull() const { return top == size - 1 ; }
    14. private:
    15. int size ; // Number of elements on Stack
    16. int top ;
    17. T* stackPtr ;
    18. } ;
    19. //constructor with the default size 10
    20. template <class T>
    21. Stack::Stack(int s)
    22. {
    23. size = s > 0 && s < 1000 ? s : 10 ;
    24. top = -1 ; // initialize stack
    25. stackPtr = new T[size] ;
    26. }
    27. // push an element onto the Stack
    28. template <class T>
    29. int Stack::push(const T& item)
    30. {
    31. if (!isFull())
    32. {
    33. stackPtr[++top] = item ;
    34. return 1 ; // push successful
    35. }
    36. return 0 ; // push unsuccessful
    37. }
    38. // pop an element off the Stack
    39. template <class T>
    40. int Stack::pop(T& popValue)
    41. {
    42. if (!isEmpty())
    43. {
    44. popValue = stackPtr[top--] ;
    45. return 1 ; // pop successful
    46. }
    47. return 0 ; // pop unsuccessful
    48. }
    49. ///
    50. //#include "stack.h"
    51. using namespace std ;
    52. int main()
    53. {
    54. typedef Stack<float> FloatStack ;
    55. typedef Stack<int> IntStack ;
    56. FloatStack fs(5) ;
    57. float f = 1.1 ;
    58. cout << "Pushing elements onto fs" << endl ;
    59. while (fs.push(f))
    60. {
    61. cout << f << ' ' ;
    62. f += 1.1 ;
    63. }
    64. cout << endl << "Stack Full." << endl
    65. << endl << "Popping elements from fs" << endl ;
    66. while (fs.pop(f))
    67. cout << f << ' ' ;
    68. cout << endl << "Stack Empty" << endl ;
    69. cout << endl ;
    70. IntStack is ;
    71. int i = 1.1 ;
    72. cout << "Pushing elements onto is" << endl ;
    73. while (is.push(i))
    74. {
    75. cout << i << ' ' ;
    76. i += 1 ;
    77. }
    78. cout << endl << "Stack Full" << endl
    79. << endl << "Popping elements from is" << endl ;
    80. while (is.pop(i))
    81. cout << i << ' ' ;
    82. cout << endl << "Stack Empty" << endl ;
    83. return 0;
    84. }

    编译运行

    C++ Shell

    结果

    1. Pushing elements onto fs
    2. 1.1 2.2 3.3 4.4 5.5
    3. Stack Full.
    4. Popping elements from fs
    5. 5.5 4.4 3.3 2.2 1.1
    6. Stack Empty
    7. Pushing elements onto is
    8. 1 2 3 4 5 6 7 8 9 10
    9. Stack Full
    10. Popping elements from is
    11. 10 9 8 7 6 5 4 3 2 1
    12. Stack Empty

    Example 2 多参数

    1. #include
    2. using namespace std;
    3. template < typename Type1, typename Type2 = double >
    4. class Point {
    5. public:
    6. // Constructor
    7. Point< Type1, Type2 >( Type1 x, Type2 y)
    8. : m_x( x ), m_y( y )
    9. {
    10. }
    11. Type2 distance()
    12. {
    13. return static_cast< Type2 >( sqrt( m_x * m_x + m_y * m_y ) );
    14. }
    15. private:
    16. Type1 m_x;
    17. Type2 m_y;
    18. };//class Point
    19. int main()
    20. {
    21. // Sample use of template function with two data types
    22. Point<int,int> pt1(2,3);
    23. Point<float> pt2(3.2,4.3);
    24. std::cout<distance()<<"\n";
    25. std::cout<distance()<<"\n";
    26. return 0;
    27. }

     

  • 相关阅读:
    济南某类国企单位面试复盘
    力扣(LeetCode)134. 加油站(C++)
    09.selenium入门
    Verilog功能模块——读写位宽不同的异步FIFO
    还没用熟 TypeScript 社区已经开始抛弃了
    数据库基础---SQL语句(基于sql server的笔记)
    uniapp 离线打包 plus.runtime.install 安装页面不弹起
    Springboot项目RestController中函数参数注解使用
    整理 js 日期对象的详细功能,使用 js 日期对象获取具体日期、昨天、今天、明天、每月天数、时间戳等,以及常用的日期时间处理方法
    Z-ARR-AMC, 90468-18-1, Cbz-Ala-Arg-Arg-AMC
  • 原文地址:https://blog.csdn.net/Ericohe/article/details/126607020