L.A*_*ida 7 c++ arrays string static const
对于用C++编写的程序,我需要两个包含数据的庞大字符串数组.
它们在头文件中定义如下:
#include <string>
static const string strdataA[30000]={"this is the first line of the data",
"the second line of data",
"other stuff in the third line",
Run Code Online (Sandbox Code Playgroud)
向下
"last line."};
//second array strings
static const string strdataB[60000]={"this is the first line of the data",
"the second line of data",
"other stuff in the third line",
Run Code Online (Sandbox Code Playgroud)
向下
"last line."};
Run Code Online (Sandbox Code Playgroud)
但是当我用g ++编译它时,我需要很长时间才能完成它.它还使用大约2 GB的虚拟内存.所以我注释掉了strdataB [],然后程序确实编译了,但是很长一段时间后仍然如此.可执行文件只有大约8 Mb并且工作正常.
我该怎么做才能加快编译过程?我不介意我是否必须更改代码,但我不想使用外部文件加载.我想要一个数组,因为它在程序中对我来说非常好.
我在网上读到"静态const"应该做的伎俩,但我从经验中学到它没有.
非常感谢您的任何建议!
你不应该使用std::string
它.使用简单旧的const char*
:
const char * const strdataA[30000] = {
"one",
"two",
//...
};
Run Code Online (Sandbox Code Playgroud)
该static
关键字不应该在这里多的差别.
这样,字符串本身将作为简单文字存储在只读数据部分中,并且数组本身将只是一个指针数组.此外,您还避免在运行时运行字符串构造函数/析构函数.