E.pXIphp

【算法随笔】写一个自己的名词空间

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了(内部已经定义了读入输出优化)。

//更新于2019/1/25

更新了mymemset。

1 template<typename value>
2 void* mymemset(void* __BASE,valve __val,unsigned ___SIZE)
3 {
4     if(__BASE==NULL || ___SIZE<0 )return NULL;
5     char* __PT=__BASE;
6     while(___SIZE--)*++__PT=val;
7     return __BASE;
8 }

 这个是真正的声明[笑cry]

 

posted @ 2019-01-21 16:51  Ekretn  阅读(291)  评论(0编辑  收藏  举报