如何在C ++中编译具有大量数据的向量? [英] How to compile a vector with huge amount of data in C++?

查看:42
本文介绍了如何在C ++中编译具有大量数据的向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C ++程序,该程序检查加泰罗尼亚语中是否存在某些单词,因此我有一个带有加泰罗尼亚语字典的向量:

I am writting a C++ program which checks if some words exist in Catalan, so I have a vector with the Catalan dictionary:

const vector<string> dict={"aaron","ababol","abac","abaca","abacallanada","abacallanava","abacas","abacial", ... ,"zum-zum","zur","zuric","zwitterio"};

问题在于字典有107776个条目,因此当我尝试编译文件时:

The problem is that the dictionary has 107776 entries, so when I attempt to compile the file:

g++ -Wall file.cc -std=c++0x -o file.exe

一段时间不执行任何操作,然后Windows表示它没有响应并关闭它.

it does nothing during a while and then Windows says that it isn't responding and closes it.

我该如何编译?有没有更好的方法来存储这种类型的数据(数组,...)?

How can I compile it? Is there a better way of storing this type of data (arrays, ...)?

推荐答案

老式的内置数组可能会给您带来更多的运气:

You may well have more luck with old-school built-in arrays:

char const * const dict[] = {"aaron",...};

这将产生大量的字符串文字和指向它们的指针数组,这对于编译器来说应该不会造成太大的负担.这也将使用不必要的更多内存,而在运行时几乎不需要工作.

This will generate a load of string literals and an array of pointers to them, which shouldn't be too much of a strain for the compiler. This will also use no more memory than necessary, with little or no work at runtime.

或者, std :: array< char const *> 应该同样有效,并且具有更多的C ++外观.

Alternatively, std::array<char const *> should be just as efficient, with more of a C++ look and feel.

您的版本还必须生成大量代码,才能从这些代码构建 initializer_list ,从每个代码构造一个字符串,并将每个字符串添加到向量中.与将每个字符串文字复制到运行时分配的内存中相比,它还需要两倍多的内存,然后所有这些指针都需要存储在另一个运行时分配的数组中.

Your version also has to generate an enormous amount of code to build an initializer_list from those, construct a string from each, and add each string to the vector. It will also require more than twice as much memory as each string literal needs to be copied into memory allocated at runtime, and then all those pointers need to be stored in another run-time allocated array.

缺点是,每次从字典中读取时,最终可能会构造一个临时字符串.如果这是一个问题,那么 std :: string 的数组可能是一个合理的折衷方案.

The disadvantage is that you may end up constructing a temporary string each time you read from the dictionary. If that's a concern, then an array of std::string might be a reasonable compromise.

这篇关于如何在C ++中编译具有大量数据的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆