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 }
  • 相关阅读:
    【网络流24题】魔术球问题(最小不相交路径覆盖)
    【网络流24题】搭配飞行员(太空飞行计划问题)(最大闭合图)
    【网络流24题】搭配飞行员(飞行员配对方案问题)(网络流)
    bzoj 1664 (贪心)
    关于正方形类问题
    就代码格式化问题
    提高组2017游记
    线程同步
    线程的优先级
    线程的常用方法
  • 原文地址:https://www.cnblogs.com/borter/p/9417890.html
Copyright © 2011-2022 走看看