构造使用字符串定义名称
Constructing define names using strings
我得到了一些我想使用的常量int
值,所以我使用了 #define。
问题是我得到了一些这样的定义名称:
#define a1 123
#define a2 6543
#define a3 12
...
代码中有没有办法以某种方式迭代它们以使用字符串构造它们的名称,然后获取该构造字符串的定义值?我的意思的伪代码:
for(int i =0 ; i < 100; i++)
{
string name = "a" + i;
func(GetDefineName(name)); // should call here every time to func with a1,a2 etc.
}
我习惯了 C#,所以我在习惯 c++ 时遇到了一些问题。定义真的是最合适的方法吗?
以#
开头的行由预处理器计算 - 编译器不会看到它们,只会看到它们的效果。因此,C++代码不能与预处理器变量交互,因为在编译代码时它们不再存在。
常量可以这样定义,但C++提供了更好的方法。例如,它们可以包装在命名空间中以避免名称冲突。
//auto lets the compiler deduce the type for you
auto const my_var = 4.5; //double
auto const close_to_pi = 3.4f; //float
auto const the_answer = 42; //int
auto const kibibyte = 1024u; //unsigned int
若要获取有关定义后缀的类型的更多说明,请查看整数文本和浮点文本。您可能还想稍后阅读 constexpr,当您更熟悉C++来探索编译时常量表达式时。
代码中有没有办法以某种方式迭代它们以使用字符串构造它们的名称,然后获取该构造字符串的定义值?
不,没有办法使用定义来做到这一点。
您需要将值放入某个容器中才能迭代这些值。
如果你只想存储值,你可以使用 std::vector。
如果你想要同时拥有名称和值,你可以使用 std::map。像这样:
#include <iostream>
#include <string>
#include <map>
#define a0 42
#define a1 5
#define a2 15
#define a3 25
int main() {
// Define the map
std::map<std::string, int> m;
// Intialize the map
m["a0"] = a0;
m["a1"] = a1;
m["a2"] = a2;
m["a3"] = a3;
// Access map using name
for(size_t i =0 ; i < m.size(); i++)
{
std::string name = "a" + std::to_string(i);
std::cout << m[name] << std::endl;
}
// Iterate all map elements
for (auto& x : m)
{
std::cout << x.first << ":" << x.second << std::endl;
}
return 0;
}
另一种方法是生成一些包含所需内容的C++文件。因此,您需要为此目的设置构建。你经常使用一些构建自动化工具(如GNU make,ninja等(,你可以配置它在需要时生成一些C++文件(例如,如果你使用make
,则在Makefile
中添加几行(。生成一些C++代码的程序可以是一些脚本(例如shell,awk,Python,Guile等(,一些专门的元程序(例如Qt程序的moc
(,其他一些预处理器,如GPP或m4,或您自己的其他C++程序等。自上个世纪以来,这种元编程方法通常与C++和C一起使用(看看Qt moc,bison,SWIG,...例如(。
另请参阅对相关问题的回答(带 C(。
你会生成一些包含以下内容的标题
#define a1 123
#define a2 6543
#define a3 12
(你不想在这样的#define
-s 中=
(
或者你可能会生成一些enum
,比如
enum number_en {
a1= 123,
a2= 6543,
a3= 12,
};
请注意,在运行时生成C++代码可能比其他解决方案更有效(自构建时以来((例如std::map
基于(方法。
宏由预处理器解析,处理器不知道(不能在代码中使用它们(。
如果要将名称与值相关联,可以使用 const map 和 constexpr 表达式:
constexpr std::string a1="a1";
constexpr std::string a2="a2";
constexpr std::string a3="a3";
const std::map<std::string, int> = {
{a1, 123},
{a2, 6543},
{a3, 12}
}
您需要 C++11(或更高版本(才能正常工作。
更喜欢常量变量而不是宏
constexpr auto a1 = 123;
constexpr auto a2 = 6543;
constexpr auto a3 = 12;
然后迭代它们,不需要名称,只需执行
for (auto e : {a1, a2, a3}) {
func(e);
}
您可能希望为列表创建一个变量,以避免在多个位置迭代时重复。
- 我无法在 qt 的C++代码中定义字符串
- 无法在模板参数中定义字符串文本
- 如何使用运算符>>在自定义字符串中输入多个单词?
- llvm,通过 C++ API 定义字符串和数组
- 字符串流中的自定义字符串输入
- 如何在Visual Studio Code tasks.json中定义C++字符串宏?
- C++ 使用链表的自定义字符串类中的重载 + 运算符
- 如何在 C++ 中安全地为 char *array 重新分配内存(它适用于自定义字符串类)
- 我可以在"setDefaultButton"中使用我自己的用户定义字符串吗
- 在编译器选项上定义字符串
- Xcode 4.5.2 libc++ std::bad_cast 实现自定义(字符串)流时
- Visual C Inliner组装程序并定义字符串
- 自定义字符串类实现建议
- 自定义字符串类中的常量字符*差异
- 如何在C++中重新定义字符串的 = 运算符
- 定义字符串时C++许多错误
- 如何在C++头文件中定义字符串参数-用Swift桥接
- 如何用C++递归地从二进制文件中读取自定义字符串
- 自定义字符串排序
- 带有自定义字符串比较器的字符串的priority_queue的C++向量