c/c++语言开发共享【算法随笔】写一个自己的名词空间

C++比C多了很多实用的东西,像class类,使得C++能够更好的OOP,struct也有了构造函数等等。还有一个东西是名词空间。想必大多数人不会对下面这句话陌生: 也就是引用了名词空间std(standard)。 看上去很高大上对不对?其实名词空间也不难写出。如下面的实例: 其实和写了一个头文件差 …

c++比c多了很多实用的东西,像class类,使得c++能够更好的oop,struct也有了构造函数等等。还有一个东西是名词空间。想必大多数人不会对下面这句话陌生:

using namespace std;

也就是引用了名词空间std(standard)。

看上去很高大上对不对?其实名词空间也不难写出。如下面的实例:

1 namespace  myfunc{  2     void swap(int &a,int &b){  3         a^=b,b^=a,a^=b;  4 }  5   #define ignb 1<<31  6 }

其实和写了一个头文件差不多

下面公布我自己的名词空间:

 1 namespace jason{   2     inline void scan(int &x){   3     int f=1;x=0;char s=getchar();   4     while(s<'0' || s>'9'){if(s=='-') f=-1;s=getchar();}   5     while(s>='0' && s<='9'){x=x*10+s-'0';s=getchar();}   6     x*=f;   7 }   8     inline void print(int x){   9         if(x<0){putchar('-');x=-x;}  10         if(x>9)print(x/10);char s=x%10+'0';  11         putchar(s);  12     }  13 struct edge_b{  14     int to,dis;  15     edge_b* nxt;  16 edge_b(int to=-1,int dis=-1,edge_b* n=null){this->to=to,this->dis=dis,this->nxt=n;}   17 };  18 }

使用时加一句using namespace jason;就ok了(内部已经定义了读入输出优化)。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/605461.html

(0)
上一篇 2021年5月13日
下一篇 2021年5月13日

精彩推荐