Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我不知道如何使用变量定义宏字符串,例如: #define str(x) "file x.txt",这意味着我希望 str(1) 引用“文件 1.txt”。但是,在这种情况下,str(1) 或任何数字指的是“文件 x.txt”,因为 x 是一个字符。 有没有办法解决这个问题?
#define str(x) "file x.txt"
连接字符串:
#define STR(x) "file " #x ".txt"
这利用了两种语言的词汇特性:相邻的字符串文字被连接;参见 C++11 2.2/6 和 C11 5.1.1.2/6:
相邻的字符串文字标记被连接起来。
#define str(x) ("file " #x ".txt")
使用字符串化运算符#
#