在C ++中定义一个'for'循环宏 [英] Define a 'for' loop macro in C++

查看:70
本文介绍了在C ++中定义一个'for'循环宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这不是一种好的编程习惯,但是是否可以为 for 循环宏定义一个变量?

Perhaps it is not good programming practice, but is it possible to define a for loop macro?

例如,

#define loop(n) for(int ii = 0; ii < n; ++ ii)

效果很好,但是不能让您更改变量名称 ii .

works perfectly well, but does not give you the ability to change the variable name ii.

可以使用:

loop(5)
{
    cout << "hi" << " " << "the value of ii is:" << " " << ii << endl;
}

但是没有名称/符号 ii 的选择.

But there is no choice of the name/symbol ii.

有可能做这样的事情吗?

Is it possible to do something like this?

loop(symbol_name, n)

程序员在其中将符号名称插入" symbol_name "中.

where the programmer inserts a symbol name into "symbol_name".

示例用法:

loop(x, 10)
{
    cout << x << endl;
}

推荐答案

#define loop(x,n) for(int x = 0; x < n; ++x)

这篇关于在C ++中定义一个'for'循环宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆