Redundant macro usage in initializer lists

穿精又带淫゛_ 提交于 2019-12-02 14:15:22

Create one array:

using arr_type=char const[];
#define ARRAY_SOURCE = arr_type{ "1", "a", "2", "b", /* etc */ }

use #ifdef to define indexes.

// elements.inc:
#ifdef A
ARRAY_SOURCE[0+OFFSET],
#endif
// etc

then

std::array<const char*, NUM + 1> arr1{{
#define OFFSET 0
#include "elements.inc"
#undef OFFSET
}};
std::array<const char*, NUM + 1> arr2{{
#define OFFSET 1
#include "elements.inc"
#undef OFFSET
}};

You might use pair/tuple to group data together:

std::array<std::pair<const char*, const char*>, NUM + 1> arr{{
#ifdef A1
    {"1", "a"},
#endif
#ifdef B2
    {"2", "b"},
#endif
#ifdef C3
    {"3", "c"},
#endif
#ifdef D4
    {"4", "d"},
#endif
#ifdef E5
    {"5", "e"},
#endif
    {"26", "z"}
}};
for (const auto& p : arr) {
    std::cout << p.first << std::endl;
}
for (const auto& str : arr) {
    std::cout << p.second << std::endl;
}
std::cout << "ok" << std::endl;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!