zoukankan      html  css  js  c++  java
  • 如果输入参数采用“指针传递”,那么加 const 修饰可以防止意外地改动该指针,起 到保护作用

    如果输入参数采用“指针传递”,那么加 const 修饰可以防止意外地改动该指针,起 到保护作用。

     1 #include <iostream>
     2 
     3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     4 using namespace std;
     5 //定义栈的尺寸
     6 const int SIZE = 100;
     7 
     8 //定义处理栈的类模板接口
     9 template <class T> class stack {
    10     T stck[SIZE];
    11     int tos;
    12 public:
    13     stack(void) {
    14         tos = 0;
    15         cout << "Stack Initialized." << endl;
    16     }
    17     ~stack(void) {
    18        cout << "Stack Destroyed." << endl;
    19     }
    20     void push(T);
    21     T pop(void);
    22 };
    23 
    24 //定义栈的成员函数
    25 template <class T> void stack<T>::push(T i)
    26 {
    27     if(tos==SIZE)
    28     {
    29         cout << "Stack is full." << endl;
    30         return;
    31     }
    32     stck[tos++] = i;
    33 }
    34 template <class T> T stack<T>::pop(void)
    35 {
    36     if(tos==0)
    37     {
    38         cout << "Stack underflow." << endl;
    39         return 0;
    40     }
    41     return stck[--tos];
    42 }
    43 
    44 //main()函数中测试stack类模板
    45 
    46 int main(int argc, char** argv) {
    47         //处理int类型数据的栈
    48     cout<<"stack<int> a :"<<endl;
    49     stack<int> a;
    50     a.push(1);
    51     a.push(2);
    52     cout << a.pop() << " ";
    53     cout << a.pop() << endl;
    54 
    55     //处理double类型数据的栈
    56     cout<<"stack<double> b :"<<endl;
    57     stack<double> b;
    58     b.push(99.3);
    59     b.push(-12.23);
    60     cout << b.pop() << " ";
    61     cout << b.pop() <<endl;
    62 
    63     //处理char类型数据的栈
    64     cout<<"stack<char> c :"<<endl;
    65     stack<char> c;
    66     for(int i=0; i<10; i++)
    67       c.push((char) 'A' + i);
    68     for(int i=0; i<10; i++)
    69       cout <<c.pop();
    70     cout << endl;
    71     return 0;
    72 }
  • 相关阅读:
    利用相关的Aware接口
    java 值传递和引用传递。
    权限控制框架Spring Security 和Shiro 的总结
    优秀代码养成
    Servlet 基础知识
    leetcode 501. Find Mode in Binary Search Tree
    leetcode 530. Minimum Absolute Difference in BST
    leetcode 543. Diameter of Binary Tree
    leetcode 551. Student Attendance Record I
    leetcode 563. Binary Tree Tilt
  • 原文地址:https://www.cnblogs.com/borter/p/9417890.html
Copyright © 2011-2022 走看看