地图和多重地图:内存使用/管理差异?

Map and multimap : memory usage / management differences?

我编写了一个程序,它必须使用运算符>>读取一个大文件,计算一些东西并在多映射中插入数据(字符串和整数)。 问题是程序崩溃了,似乎是内存问题(对小文件效果很好)。 我认为它达到了 Windows.

的内存分配限制

我尝试了什么: 我制作了 32 位和 64 位版本。 如您所知,Windows 的内存分配限制对于 32 位和 64 位版本不同。 两者都崩溃了。 我查看了任务管理器的性能选项卡以观察内存消耗。 32 位:程序每次达到 5.3 GB 时都会崩溃。 64位:程序内存消耗不断增加,直到达到系统的整个RAM并崩溃。

我尝试使用 map 而不是 multimap:程序从不崩溃并且在整个程序执行过程中保持稳定在大约 3.5 GB(32 位和 64 位版本)。

那么,您知道 map 和 multimap 在内存使用或内存管理方面是否存在差异吗? 或者仅仅是因为地图不允许重复键?

当然有。你读过描述了吗?

std::map:

std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare.

std::map::insert:

Return value: 1-2) Returns a pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool denoting whether the insertion took place.

你不能有两个相同的键,所以你覆盖了同一个键下的值,你不分配任何新的space。

std::multimap:

Multimap is an associative container that contains a sorted list of key-value pairs.

std::multimap::insert 不能 "fail",它总是 return 一个迭代器,因为它总是添加新的关联。所以在循环中添加相同键下的项目将分配新的内存。

每个多容器(集合、无序映射、)也是如此。它们在语义上是不同的。