如何在定义函数中使用 int 值

How to use int value in define function

在我的程序中,我有很多变量,其名称包括:单词+数字。 例如,如果我 rand() 数字 6 - 我必须迭代 word6。 我试图创建一个定义函数来让我更容易,但我不知道如何解析参数的值。

#define it(c,t) c ## t

*in main:*
int c1=0 ,t=1;
it(c , t)++;
cout<<c1;

你能告诉我如何让它工作吗?

您可能不想这样做。考虑使用数组:

int word[6];  // Declares 6 int variables
word[0] = 1;  // Access them with an index, 0 is the first one
word[5] = 42; // Access the last one.

这样使用的字符更少,并且其他人希望您这样做。这可能就是你想要的。