一个STL字符串类可以容纳多少个字符?

发布于 2024-10-06 01:07:32 字数 213 浏览 1 评论 0原文

我需要与一系列角色合作。字符数最多可达 1011。 在通常的数组中,这是不可能的。我应该用什么? 我想使用 gets() 函数来保存字符串。但是,这对于 STL 容器来说可能吗? 如果没有的话,那有什么办法呢?

例子: 输入: 阿米拉希德 输出:AMIRAHID

如果 32 位机器中的字符数减少到 10^10,如何实现这一点?

先感谢您。

I need to work with a series of characters. The number of characters can be upto 1011.
In a usual array, it's not possible. What should I use?
I wanted to use gets() function to hold the string. But, is this possible for STL containers?
If not, then what's the way?

Example:
input:
AMIRAHID
output: A.M.I.R.A.H.I.D

How to make this possible if the number of characters lessened to 10^10 in 32-bit machine ?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烧了回忆取暖 2024-10-13 01:07:32

嗯,这大约是 100GByte 的数据。通常的字符串类都无法容纳超过主内存的内容。您可能需要查看 STXXL,它是 STL 的实现,允许在磁盘上存储部分数据。

Well, that's roughly 100GByte of data. No usual string class will be able to hold more than fits into your main memory. You might want to look at STXXL, which is an implementation of STL allowing to store part of the data on disk.

债姬 2024-10-13 01:07:32

如果您的机器有 1011 == 93GB 内存,那么它可能是 64 位机器,因此 string 可以工作。否则什么也帮不了你。

已编辑问题的已编辑答案:在这种情况下,您实际上不需要将整个字符串存储在内存中。您只能存储适合内存的一小部分。

只需从输入中读取每个字符,将其写入输出并在其后写入一个点。重复此操作,直到输入出现 EOF。为了提高性能,您可以读取和写入大块数据,但仍然可以放入内存中。

此类算法称为在线算法

If your machine has 1011 == 93GB of memory then it's probably a 64bit machine, so string will work. Otherwise nothing will help you.

Edited answer for the edited question: In that case you don't really need to store the whole string in memory. You can store only small part of it that fits into the memory.

Just read every character from the input, write it to the output and write a dot after it. Repeat it until you get and EOF on the input. To increase performance you can read and write large chunks of the data but such that still can fit into the memory.

Such algorithms are called online algorithms.

物价感观 2024-10-13 01:07:32

创建这么大的数组是可能的。但在 32 位机器上不行。切换到 STL 可能没有帮助,而且没有必要。

It is possible for an array that large to be created. But not on a 32-bit machine. Switching to STL will likely not help, and is unnecessary.

洒一地阳光 2024-10-13 01:07:32

您需要考虑有多少内存,以及您是否有机会这样做。

1011 大约为 100 GB,这意味着您需要 64 位系统(和编译器)才能对其进行寻址。

STL 的字符串最多支持 max_size() 个字符,因此答案可能会随着实现而改变。

You need to contemplate how much memory that is, and if you have any chance of doing it at all.

1011 is roughly 100 gigabytes, which means you will need a 64-bit system (and compiler) to even be able to address it.

STL's strings support a max of max_size() characters, so the answer can change with the implementation.

电影里的梦 2024-10-13 01:07:32

字符串与数组有同样的问题:*它必须适合内存

10^11 个字符将占用超过 4GB。这很难适应具有 4GB 内存空间的 32 位机器的内存。您要么需要将数据分割成更小的块,并且一次只加载其中的一部分,要么切换到 64 位,在这种情况下,数组和字符串都应该能够保存数据(尽管最好将其分成多个较小的字符串/数组

A string suffers from the same problem as an array: *it has to fit in memory.

10^11 characters would take up over 4GB. That's hard to fit into memory on a 32-bit machine which has a 4GB memory space. You either need to split up your data into smaller chunks, and only load a bit of it at a time, or switch to 64-bit, in which case both arrays and strings should be able to hold the data (although it may still be preferable to split it up into multiple smaller strings/arrays

醉生梦死 2024-10-13 01:07:32

STL 的 SGI 版本有一个 ROPE 类(绳子是一根大绳子,明白了)。

我不确定它的设计目的是处理那么多数据,但你可以看一下。
http://www.sgi.com/tech/stl/Rope.html

The SGI version of STL has a ROPE class (A rope is a big string, get it).

I am not sure it is designed to handle that much data but you can have a look.
http://www.sgi.com/tech/stl/Rope.html

场罚期间 2024-10-13 01:07:32

如果您想要做的只是读取某个大文件并将相同的数据写入另一个文件,并且每个字符之间散布有句点,那么为什么要麻烦一次将整个数据读入内存呢?选择一些合理的缓冲区大小并分块进行。

If all you're trying to do is read in some massive file and write to another file the same data with periods interspersed between each character, why bother reading the whole thing into memory at once? Pick some reasonable buffer size and do it in chunks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文