如何实现用gcc-4.4编译的大向量初始化?

How to implement large vector initialization that compiles with gcc-4.4?

我有一个包含 20k 个已知字符串的列表,这些字符串在编译时就知道并且永远不会更改。一种不可配置的字典。我不想在 运行 时间内从文件中加载它,因为这意味着很多不必要的架构:在特定路径中查找文件、指示路径的配置文件等。

我在 C++ 中想出了这样的解决方案:

在a.cpp中:

std::vector<std::string> dic;
dic.reserve(20000);
#define VECTOR_DIC_ dic;
#include values.inl
#undef VECTOR_DIC_

然后在 values.inl 中,一列 20k push_back 调用,如下所示:

VECTOR_DIC_.push_back("string1");
VECTOR_DIC_.push_back("string2");
...
VECTOR_DIC_.push_back("string20000");

此代码在 Debian 上使用 gcc-4.8 编译并正常工作,但无法使用 gcc-4.4 编译,gcc-4.4 从未完成编译 a.cpp 文件。

为什么gcc-4.4不支持这种大初始化?另外,是否有在编译时对已知值进行如此大的初始化的设计模式?

编译器可能会犹豫,因为初始化不在函数内部。

要使其工作,请在函数中插入初始化器。

如:

std::vector<std::string> dic;  // wouldn't an std::set be a better match?

bool InitDitionary() {
  dic.reserve(20000);
  #define VECTOR_DIC_ dic;
  #include values.inl
  #undef VECTOR_DIC_
  return true;
}

// you can then call InitDictionary at your discretion from within your app
// or the following line will initialize before the call to main()
bool bInit = InitDictionnary();

或者,static const char* 替代方案也是可行的,您必须将字符串文件更改为这种格式,我建议您包括整个声明,因为它可能是由软件生成的。该数组应该预先排序,因此您可以使用 binary_search、upper_bound 等搜索它....

const char dic[20000] = {  // <-- optional, in the file, so you have the number of items 
    "string1",
    "string2",
    "string3",
    "string4",
    // ...
};
const size_t DIC_SIZE = sizeof(dic) / sizeof(dic[0]);  // :)

您可以给文件一个 .cpp 扩展名,或包含为:

#include "dictionary.inc"

使用 const char * 的数组,然后从中初始化向量:

#include <string>
#include <vector>

char const * const S[] = {
    "string1",
    "string2"
};

const std::size_t N_STRINGS = sizeof(S) / sizeof(*S);

const std::vector<std::string> dic(S, S + N_STRINGS);

使用 g++ 4.4.7.

可以很好地编译(虽然没有用 20k 字符串进行测试)