连接字符串中的宏 - C ++ [英] Concatenating strings in macros - C++
本文介绍了连接字符串中的宏 - C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是串联在宏定义的字符串最简单的方法。
即伪code我要找的是这样的:
What's the easiest way to concatenate strings defined in macros. i.e. The pseudo code I'm looking for would be like:
#define ROOT_PATH "/home/david/"
#define INPUT_FILE_A ROOT_PATH+"data/inputA.bin"
#define INPUT_FILE_B ROOT_PATH+"data/inputB.bin"
...
#define INPUT_FILE_Z ROOT_PATH+"data/inputZ.bin"
我知道的唯一方法是在code或使用String类,然后c_str方法使用strcat的,但它可以变得混乱时,我有很多的输入文件。我想只使用INPUT_FILE_A等,而不是直接有很多局部变量。有没有做到这一点的好办法?
The only way I know of is to use strcat in the code, or using the string class and then the c_str method, but it can get messy when I have lots of input files. I'd like to just use INPUT_FILE_A, etc. directly and not have lots of local variables. Is there a good way to do this?
感谢。
推荐答案
编译器会自动串联邻近的字符串:
The compiler will automatically concatenate adjacent strings:
#define ROOT_PATH "/home/david/"
#define INPUT_FILE_A ROOT_PATH "data/inputA.bin"
或多个通用:
#define INPUT_FILE_DETAIL(root,x) root #x
#define INPUT_FILE(x) INPUT_FILE_DETAIL(ROOT_PATH "data/", x)
这篇关于连接字符串中的宏 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文