CC++用宏定义实现简单泛型的功能

更新时间:2023-07-03 09:34:07 阅读: 评论:0

CC++⽤宏定义实现简单泛型的功能
#include <iostream>
witchcraft
#include <cstdio>
#include <cstdlib>
#include <asrt.h>
using namespace std;
//宏定义实现泛型
//在宏定义中出现#和##,通常起到下⾯的作⽤:
// #表⽰:对应变量字符串化
// ##表⽰:把宏参数名与宏定义代码序列中的标识符连接在⼀起,形成⼀个新的标识符
#define GNERIC_STACK(STACK_TYPE,SUFFIX,STACK_SIZE)  \
static STACK_TYPE stack##SUFFIX[STACK_SIZE];    \
static int top_element##SUFFIX=-1;      \
铲车培训学校bool is_empty##SUFFIX(){      \
return top_element##SUFFIX==-1;      \
}                                \
\
bool is_full##SUFFIX(){      \
return top_element##SUFFIX==STACK_SIZE-1; \
}            \
\
void push##SUFFIX(STACK_TYPE val){    \
asrt(!is_full##SUFFIX());    \
新车如何保养
top_element##SUFFIX+=1;      \
colour怎么读stack##SUFFIX[top_element##SUFFIX]=val;  \
}            \
广东高考语文\
void pop##SUFFIX(){        \
asrt(!is_empty##SUFFIX());    \
top_element##SUFFIX-=1;      \
}            \
\
STACK_TYPE top##SUFFIX(){      \
asrt(!is_empty##SUFFIX());      \
return stack##SUFFIX[top_element##SUFFIX]; \
一对一口语}
//注意,下⾯的宏定义式⼦不能放到main()函数内,因为这个宏定义展开后包含其他函数的定义,故不能
//宏定义⽣成两个不同类型的堆栈
//其结尾加不加';'都可以,注意下⾯的宏定义会被展开成什么(⽂本替换)
GNERIC_STACK(int,_int,10)//##起到连接作⽤,⽐如is_empty##SUFFIX(),SUFFIX为_int,即最后⽣成is_empty_int() GNERIC_STACK(double,_double,10)//同上
void test(){
push_int(5);
push_int(10);path是什么意思
admit的用法push_int(22);
push_double(22.2);
push_double(-33.3);
push_double(-45.4);
accordancewhile(!is_empty_int()){
cout<<top_int()<<" ";
pop_int();
}
cout<<endl;
while(!is_empty_double()){
cout<<top_double()<<" ";
pop_double();
mssl}
cout<<endl;
}
}
int main() { test();
return 0; }

本文发布于:2023-07-03 09:34:07,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1075822.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:定义   泛型   实现
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图