如何在c / c ++中存储大字符串,大小接近10 ^ 6个字符 [英] how to store large strings in c/c++ having size nearly 10^6 characters
本文介绍了如何在c / c ++中存储大字符串,大小接近10 ^ 6个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在c / c ++中存储大字符串,大小接近10 ^ 6个字符
how to store large strings in c/c++ having size nearly 10^6 characters
推荐答案
如果你事先知道确切的大小并且它不会改变,只需使用new
。你应该能够毫无问题地分配一千倍。
如果您事先不知道确切的大小,或者字符串内容(和大小) )会改变,然后在理论上你应该使用类std :: string
。如果需要,它将负责(重新)分配。也就是说,如果您创建一组结构合理的较小字符串,那么任何更改都将花费相当多的时间来处理单个大字符串。例如,如果字符串包含文本文件的内容,则将每一行存储在单独的字符串变量中更为明智。
If you know the exact size in advance and it won't change, just usenew
. You should be able to allocate a thousand times as much without a problem.
If you don't know the exact size in advance, or the string contents (and size) will change, then in theory you should use the classstd::string
instead. It will take care of (re)allocations, if needed. That said, any change will take considerably more time for a single large string, than it would take if you created a properly structured set of smaller strings instead. If for instance the string holds the contents of a text file, it would be more sensible to store each line inside a separate string variable.
只需使用<$ c $分配足够大小的缓冲区c> new (C ++)或 malloc
(C)。
Just allocate buffers with sufficient size usingnew
(C++) ormalloc
(C).
我会查看标准模板库std :: string
http://www.cprogramming.com/tutorial/string.html [ ^ ]
I would look at the Standard Template Library std::string
http://www.cprogramming.com/tutorial/string.html[^]
这篇关于如何在c / c ++中存储大字符串,大小接近10 ^ 6个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文