简体   繁体   English

在头文件中包含大量Vector

[英]Including a massive Vector in header file

So I have a massive vector that I load line by line from a file in C++ using an std::ifstream object. 因此,我有一个庞大的向量,可以使用std :: ifstream对象从C ++文件中逐行加载。 This works, but I was thinking, hey why don't I write the vector to a .cpp file like below and just #include it as a header: 这可行,但是我在想,为什么我不将向量写到如下的.cpp文件中,而只是将其作为头包含在内:

std::vector<std::vector<std::string>> vectorOfGates2 = { {
"Q_OR04 g359(.A0 (i2), .A1 (i1), .A2 (i0), .A3 (i3), .Z (ptm_replace_0));",
},
{
"Q_OR04 g357(.A0 (i2), .A1 (i1), .A2 (ptm_replace_0), .A3 (i3), .Z (ptm_replace_1));",
"Q_INV g358(.A (i0), .Z (ptm_replace_0));",
},
{
"Q_OR04 g357(.A0 (i2), .A1 (i0), .A2 (ptm_replace_0), .A3 (i3), .Z (ptm_replace_1));",
"Q_INV g358(.A (i1), .Z (ptm_replace_0));",
}};

When I do this with a small vector and #include the file it works (vectorOfGates is in a file called vector.cpp that I just include). 当我使用一个较小的vector并#include这个文件时,它就可以工作(vectorOfGates位于我刚刚包含的名为vector.cpp的文件中)。 However GCC gives me a segmentation violation error if I try it with a big vector (it takes like three minutes of trying to build to cause this error: 但是,如果我尝试使用大向量,则GCC会给我一个细分违规错误(尝试进行构建可能需要三分钟才能导致此错误:

 22%] Building CXX object CMakeFiles/framework_roman_src_rtl.dir/MappingSingleton.cpp.o
/grid/common/pkgs/gcc/v6.3.0p2/bin/g++  -DBUILD_TO_RUN_WITH_XCELIUM -DET6=ET6 -DLINUX2 -D_GLIBCXX_USE_CXX11_ABI=0 -I/grid/cva/p4_02/hisham/gcc/ua/Framework/Roman/src/x86-lx2-64 -I/grid/cva/p4_02/hisham/gcc/ua/Framework/Roman/src -I/grid/cva/p4_02/hisham/gcc/ap/include -I/grid/cva/p4_02/hisham/gcc/ua/Framework/include -I/grid/cva/p4_02/hisham/gcc/ua/include -I/grid/cva/p4_02/hisham/gcc/sys/avs/xlm/19.03/s1/include -I/grid/cva/p4_02/hisham/gcc/sys/include/x86-lx2-64  -std=c++14 -g   -pthread -g -Wall -Werror -std=gnu++14 -o CMakeFiles/framework_roman_src_rtl.dir/MappingSingleton.cpp.o -c /grid/cva/p4_02/hisham/gcc/ua/Framework/Roman/src/MappingSingleton.cpp
g++: internal compiler error: Segmentation fault (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
gmake[2]: *** [CMakeFiles/framework_roman_src_rtl.dir/MappingSingleton.cpp.o] Error 4
gmake[2]: Leaving directory `/grid/cva/p4_02/hisham/gcc/ua/Framework/Roman/src/cmake-build-debug'
gmake[1]: *** [CMakeFiles/framework_roman_src_rtl.dir/all] Error 2
gmake[1]: Leaving directory `/grid/cva/p4_02/hisham/gcc/ua/Framework/Roman/src/cmake-build-debug'
gmake: *** [all] Error 2

What am I doing wrong? 我究竟做错了什么? The same size vector loads if I try doing it using a ifstream line by line, so I don't wknow what's different here. 如果我尝试逐行使用ifstream来加载相同大小的向量,那么我不知道这里有什么不同。 Thanks. 谢谢。

One possibility is to declare you array in a way that only uses static data, with no constructors needing to be called. 一种可能是以仅使用静态数据的方式声明数组,而无需调用构造函数。 Then the entire thing will be compile time data. 那么整个事情就是编译时间数据。 Something like 就像是

const char *vectorOfGates2[][MAX_SIZE] = {
    { "string1" },
    { "string2", "string3" },
    // ...
};

Where MAX_SIZE is a constant that holds the maximum number of entries for any particular row of the vector. 其中MAX_SIZE是一个常数,用于保存向量的任何特定行的最大条目数。

If necessary, this can be converted to a std::vector<std::vector<std::string>> (or std::vector<std::vector<const char *>> ) upon program startup, depending on how you need to use it. 如有必要,可以在程序启动时将其转换为std::vector<std::vector<std::string>> (或std::vector<std::vector<const char *>> ),具体取决于您需要使用它。 But leaving it as the static array would use the least amount of memory, as no dynamic memory would need to be allocated. 但是将其保留为静态数组将使用最少的内存,因为不需要分配动态内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM