码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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. }

     

  • 相关阅读:
    pdf 转 word
    mac 升级node到指定版本
    正版软件 | R-Drive Image:数据安全守护者,您的智能备份专家
    Request processing failed: com.microsoft.playwright.PlaywrightException: Error
    Linux UART编程 驱动蓝牙芯片
    java计算机毕业设计高校在线办公系统源码+数据库+系统+lw文档+mybatis+运行部署
    CentOS | 添加普通用户并授权sudo
    马斯克搞脑机得“开瓢”?MIT 早在研究「挂耳式耳机」,戴上=“把整个互联网装进脑子”!...
    2023-11-04:用go语言,如果n = 1,打印 1*** 如果n = 2,打印 1*** 3*** 2*** 如果n = 3,打印
    什么是数据库的读写分离?什么是主表从表,主库从库?
  • 原文地址:https://blog.csdn.net/Ericohe/article/details/126607020
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号