地图和多图:内存使用/管理差异? [英] Map and multimap : memory usage / management differences?

查看:150
本文介绍了地图和多图:内存使用/管理差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序,必须读取一个大文件与操作符>>,计数一些东西,并插入数据(字符串和int)在多图。
问题是程序崩溃,它似乎是一个内存问题(它适用于小文件)。
我认为它达到了Windows的内存分配限制。

I wrote a program that have to read a big file with operator>> , count some stuff and insert datas (string and int) in a multimap. The problem is that the program crashes and it seems to be a memory problem (it works well with small files). I think that it reaches the memory allocation limit of Windows.

我试过:
我做了一个32位和一个64位版本。
如你所知,Windows有一个内存分配限制,对于32位和64位内部版本是不一样的。
两个都崩溃了。
我查看任务管理器的性能选项卡以观察内存消耗。
32位:每次达到5.3 GB时程序崩溃。
64位:程序的内存消耗持续增加,直到它到达系统的整个RAM并崩溃。

What I tried : I made a 32-bit and an 64-bit versions. As you know, Windows has a memory allocation limit that is not the same for 32bits and 64bits builds. With both it crashes. I looked the performance tab of the task manager to observe the memory consumption. 32-bit : the program crashes every time it reaches 5.3 GB. 64-bit : the memory consumption of the program continually increases until it reaches the whole RAM of the system and crashes.

我试图用一个映射代替a multimap:程序永远不会崩溃,并在程序执行时保持稳定在大约3.5 GB(32位和64位版本)。

I tried with a map instead of a multimap : the program never crashes and stay stable at roughly 3.5 GB (32-bit and 64-bit builds) all along the program execution.

如果在内存使用或内存管理方面map和multimap之间有区别?
或者是因为map不允许重复键?

So, do you know if there is a difference between map and multimap in terms of memory usage or memory management ? Or is it just because map do not allows duplicate keys ?

推荐答案

您是否已阅读说明?

std :: map


std :: map是一个排序关联容器,其中包含具有唯一键的键值对。键使用比较函数Compare进行排序。

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

返回值
1-2)返回一个由迭代器组成的插入元素(或阻止插入的元素)

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.

您不能有两个相同的键,因此您可以使用相同的键重写值不分配任何新空间。

You cannot have two same keys, so you overwrite values under the same key, you don't allocate any new space.

std :: multimap


Multimap一个包含键值对的排序列表的关联容器。

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

std :: multimap :: insert 无法失败一个迭代器因为,它总是添加新的关联。因此在循环中在同一个键下添加项将分配新的内存。

std::multimap::insert cannot "fail", it always return an iterator because, it always adds new association. So adding items under the same key in a loop will allocate new memory.

对于每个多容器(集合,无序映射, / em>)。它们在语义上不同。

Similar is true for every multi-container (set, unordered map, etc.). They are semantically different.

这篇关于地图和多图:内存使用/管理差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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