“multiset” & “multimap” - What's the point?

时间:2022-11-27 20:46:50

As the question states ... I don't get the point about multisets / multimaps.

正如问题所述...我不明白多点/多图。

So, what's the purpose?

那么,目的是什么?

7 个解决方案

#1


28  

Some use cases:

一些用例:

multimap

多重映射

  • With ZIP code as a key, all people which have that ZIP code
  • 使用邮政编码作为密钥,所有拥有该邮政编码的人
  • With account ID as key, all open orders of that person/account
  • 使用帐户ID作为密钥,该人/帐户的所有未结订单
  • A dictionary, with per keyword various explanations
  • 一本字典,每个关键字有各种解释

multiset

多集

is in essence a map with a key and a integer count.

本质上是一个带有键和整数计数的地图。

  • The inventory of a shop, all products have their key and the amount still available is the value
  • 一个商店的库存,所有产品都有他们的钥匙,仍然可用的金额是价值
  • accumulated sales data of a shop, every time a product is sold the product id get's added to the multiset thereby increasing the amount sold
  • 商店的累计销售数据,每次销售产品时,产品ID都会添加到多页,从而增加销售量

#2


2  

One example where a multimap would be useful if you had a situation where most of the time the keys are unique, but sometimes they aren't.

一个例子,如果你的大多数情况下密钥都是唯一的,那么多图会很有用,但有时却不是。

For example, if you were creating a cache class that used a hash as a key. Most of the time two different objects will not have the same hash, so the keys will be unique. But it is possible that you will get hash collisions for different objects, so you would want a multimap to cover that situation.

例如,如果您正在创建一个使用哈希作为键的缓存类。大多数情况下,两个不同的对象不会具有相同的哈希值,因此键将是唯一的。但是有可能你会得到不同对象的哈希冲突,所以你需要一个多图来覆盖这种情况。

Another example would be any sort of non-unique index (like in a database).

另一个例子是任何类型的非唯一索引(如数据库中)。

As for a multiset - I think those would be less useful. Only thing I can think of would be to use it as a kind of automatically sorted list.

至于多重集 - 我认为那些不太有用。我唯一能想到的就是将它用作一种自动排序的列表。

#3


2  

A multiset or multimap is simply for situations where there might be more than one of a particular item. For example, let's say you wanted to create an index for a book. You'd scan through the text, throw out all the really common meaningless words ("a", "an", "the", etc.) and then make a list of all the rest, and the place in the book where each occurred.

多集或多图仅适用于可能存在多个特定项的情况。例如,假设您想为书籍创建索引。你会扫描文本,扔出所有真正常见的无意义的词(“a”,“an”,“the”等),然后列出所有其余的,以及书中的每个地方发生。

Quite a few of the words will appear on more than one page, in which case you'll have multiple entries mapping from one word to different pages. One way to handle that would be a multimap from words to page numbers.

相当多的单词将出现在多个页面上,在这种情况下,您将有多个条目从一个单词映射到不同的页面。处理这种情况的一种方法是从单词到页码的多重映射。

#4


1  

http://www.cplusplus.com/reference/stl/multimap/

http://www.cplusplus.com/reference/stl/multimap/

Maps are a kind of associative containers that stores elements formed by the combination of a key value and a mapped value, much like map containers, but allowing different elements to have the same key value.

映射是一种关联容器,它存储由键值和映射值组合形成的元素,非常类似于映射容器,但允许不同的元素具有相同的键值。

It is kind of registry where elements can share a key. You can think of companies and employees. Street address is a key and employees are values.

它是一种注册表,元素可以共享一个键。你可以想到公司和员工。街道地址是关键,员工是价值观。

#5


0  

Here's what Wikipedia says about uses:

以下是*所说的用途:

  • In a student enrollment system, where students may be enrolled in multiple classes simultaneously, there might be an association for each enrollment of a student in a course, where the key is the student ID and the value is the course ID. If a student is enrolled in three courses, there will be three associations containing the same key.
  • 在学生注册系统中,学生可以同时注册多个班级,在课程中学生的每次注册可能存在关联,其中关键是学生ID,值是课程ID。如果学生注册了三门课程,则会有三个包含相同密钥的关联。
  • The index of a book may report any number of references for a given index term, and thus may be coded as a multimap from index terms to any number of reference locations
  • 书的索引可以报告给定索引词的任意数量的引用,因此可以编码为从索引词到任意数量的引用位置的多图。

#6


0  

Use multimap, wherever you want to use tree kind of a structure.

使用multimap,无论您想在何处使用树类结构。

#7


0  

The most important benefit of using a multiset over a vector/list(or any other container) is the time complexity of find operation. average case time complexity for multiset is O(logn) and unordered_multiset is O(1). Same is true for multimap and ordered_multimap.

在向量/列表(或任何其他容器)上使用多集的最重要的好处是查找操作的时间复杂性。 multiset的平均大小写时间复杂度为O(logn),unordered_multiset为O(1)。 multimap和ordered_multimap也是如此。

#1


28  

Some use cases:

一些用例:

multimap

多重映射

  • With ZIP code as a key, all people which have that ZIP code
  • 使用邮政编码作为密钥,所有拥有该邮政编码的人
  • With account ID as key, all open orders of that person/account
  • 使用帐户ID作为密钥,该人/帐户的所有未结订单
  • A dictionary, with per keyword various explanations
  • 一本字典,每个关键字有各种解释

multiset

多集

is in essence a map with a key and a integer count.

本质上是一个带有键和整数计数的地图。

  • The inventory of a shop, all products have their key and the amount still available is the value
  • 一个商店的库存,所有产品都有他们的钥匙,仍然可用的金额是价值
  • accumulated sales data of a shop, every time a product is sold the product id get's added to the multiset thereby increasing the amount sold
  • 商店的累计销售数据,每次销售产品时,产品ID都会添加到多页,从而增加销售量

#2


2  

One example where a multimap would be useful if you had a situation where most of the time the keys are unique, but sometimes they aren't.

一个例子,如果你的大多数情况下密钥都是唯一的,那么多图会很有用,但有时却不是。

For example, if you were creating a cache class that used a hash as a key. Most of the time two different objects will not have the same hash, so the keys will be unique. But it is possible that you will get hash collisions for different objects, so you would want a multimap to cover that situation.

例如,如果您正在创建一个使用哈希作为键的缓存类。大多数情况下,两个不同的对象不会具有相同的哈希值,因此键将是唯一的。但是有可能你会得到不同对象的哈希冲突,所以你需要一个多图来覆盖这种情况。

Another example would be any sort of non-unique index (like in a database).

另一个例子是任何类型的非唯一索引(如数据库中)。

As for a multiset - I think those would be less useful. Only thing I can think of would be to use it as a kind of automatically sorted list.

至于多重集 - 我认为那些不太有用。我唯一能想到的就是将它用作一种自动排序的列表。

#3


2  

A multiset or multimap is simply for situations where there might be more than one of a particular item. For example, let's say you wanted to create an index for a book. You'd scan through the text, throw out all the really common meaningless words ("a", "an", "the", etc.) and then make a list of all the rest, and the place in the book where each occurred.

多集或多图仅适用于可能存在多个特定项的情况。例如,假设您想为书籍创建索引。你会扫描文本,扔出所有真正常见的无意义的词(“a”,“an”,“the”等),然后列出所有其余的,以及书中的每个地方发生。

Quite a few of the words will appear on more than one page, in which case you'll have multiple entries mapping from one word to different pages. One way to handle that would be a multimap from words to page numbers.

相当多的单词将出现在多个页面上,在这种情况下,您将有多个条目从一个单词映射到不同的页面。处理这种情况的一种方法是从单词到页码的多重映射。

#4


1  

http://www.cplusplus.com/reference/stl/multimap/

http://www.cplusplus.com/reference/stl/multimap/

Maps are a kind of associative containers that stores elements formed by the combination of a key value and a mapped value, much like map containers, but allowing different elements to have the same key value.

映射是一种关联容器,它存储由键值和映射值组合形成的元素,非常类似于映射容器,但允许不同的元素具有相同的键值。

It is kind of registry where elements can share a key. You can think of companies and employees. Street address is a key and employees are values.

它是一种注册表,元素可以共享一个键。你可以想到公司和员工。街道地址是关键,员工是价值观。

#5


0  

Here's what Wikipedia says about uses:

以下是*所说的用途:

  • In a student enrollment system, where students may be enrolled in multiple classes simultaneously, there might be an association for each enrollment of a student in a course, where the key is the student ID and the value is the course ID. If a student is enrolled in three courses, there will be three associations containing the same key.
  • 在学生注册系统中,学生可以同时注册多个班级,在课程中学生的每次注册可能存在关联,其中关键是学生ID,值是课程ID。如果学生注册了三门课程,则会有三个包含相同密钥的关联。
  • The index of a book may report any number of references for a given index term, and thus may be coded as a multimap from index terms to any number of reference locations
  • 书的索引可以报告给定索引词的任意数量的引用,因此可以编码为从索引词到任意数量的引用位置的多图。

#6


0  

Use multimap, wherever you want to use tree kind of a structure.

使用multimap,无论您想在何处使用树类结构。

#7


0  

The most important benefit of using a multiset over a vector/list(or any other container) is the time complexity of find operation. average case time complexity for multiset is O(logn) and unordered_multiset is O(1). Same is true for multimap and ordered_multimap.

在向量/列表(或任何其他容器)上使用多集的最重要的好处是查找操作的时间复杂性。 multiset的平均大小写时间复杂度为O(logn),unordered_multiset为O(1)。 multimap和ordered_multimap也是如此。