zoukankan      html  css  js  c++  java
  • c++ namespace简单用法

    提供一个小例子,例子来源于《c++程序设计语言》,总共包含3个文件,分别是命名空间声明的文件Stack.h,命名空间的实现文件Stack.cpp,以及命名空间的使用文件main.c

    下面是相关代码:

    Stack.h:

    1 namespace Stack{
    2     void push(int e);
    3     int pop();
    4 }

    Stack.cpp:

     1 #include"stack.h"
     2 
     3 namespace Stack{
     4     const int max_size=100;
     5     int sstack[max_size];
     6     int top=0;
     7     void push(int e){
     8         if(top<max_size){
     9             sstack[top++]=e;
    10         }
    11     }
    12     int pop(){
    13         if(top>0){
    14             --top;
    15         }
    16         return sstack[top];
    17     }
    18 }

     1 #include"stack.h"
     2 
     3 namespace Stack
     4 {
     5 const int max_size=100;
     6 int sstack[max_size];
     7 int top=0;
     8 }
     9 
    10 void Stack::push(int e)
    11 {
    12     if(top<max_size)
    13     {
    14         sstack[top++]=e;
    15     }
    16 }
    17 int Stack::pop()
    18 {
    19     if(top>0)
    20     {
    21         --top;
    22     }
    23     return sstack[top];
    24 }

    main.cpp:

     1 #include "stack.h"
     2 #include <iostream>
     3 
     4 using namespace std;
     5 using namespace Stack;
     6 
     7 int main()
     8 {
     9     push(5);
    10     cout<<pop();
    11     return 0;
    12 }

    运行结果:

  • 相关阅读:
    电商工具 谷歌插件 版本 2021-03-04
    PowerDesigner 自定义脚本
    MapReduce案例之寻找共同好友
    Hadoop之MapReduce开发总结
    python之文件操作
    python字典、集合
    python元组
    python列表练习
    python之列表
    python之编码解码、字符串常用方法
  • 原文地址:https://www.cnblogs.com/caoyingjie/p/3875478.html
Copyright © 2011-2022 走看看