Questions tagged [unordered-multimap]

An associative container that contains pairs of key-value elements in which multiple values can be mapped to the same key. Descends from unordered_map data structure and have similar complexity in operations.

An unordered_multimap is a special kind of unordered_map in which multiple values can be mapped to the same key.

It's purpose and implementation is similar to an unordered_map:

  • the container doesn't guarantee any specific order of the elements
  • key-value pairs are stored into buckets
  • checking if a specific key has at least a value has constant complexity
  • checking if a specific key has a specific value has linear complexity relative to the amount of values mapped to the specific key
23 questions
10
votes
2 answers

What are some uses of local iterator for STL unordered containers?

In §23.2.7 Unordered associative containers [unord.req] of the C++ standard table 91 describes the additional requirements a STL unordered associative container must meet. In this table the standard dictates that the STL unordered containers (i.e.,…
101010
  • 41,839
  • 11
  • 94
  • 168
6
votes
1 answer

Guarantees about key uniqueness in std::unordered_multimap

I was wondering about uniqueness of a key object inside an std::unordered_multimap when dealing with iteration. I'll try to explain the point: I need to associate some data with the key type in the map, this data should not be considered in Hash or…
Jack
  • 131,802
  • 30
  • 241
  • 343
4
votes
1 answer

Efficient node extract() + insert() in `unordered_map`

Using the C++17 node-extract() functions, I can change the key without having to re-allocate the node. In my particular use-case, I'm replacing the key with an equal one, so I'd like to use insert()-with-hint to avoid a full second lookup, and that…
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
4
votes
2 answers

Random unordered_multimap using std::generate

I'm trying to generate a random unordered_multimap of size 10 using the following code: #include #include #include int main() { auto m = std::unordered_multimap(10); std::generate( …
4
votes
1 answer

Printing multiple values of particular key in std::unordered_multimap in C++

I am trying to print all the values associated with the particular key in unordered_multiset in C++ but unfortunately, when I run bellow code, I am getting two different outputs in Visual studio and online compiler http://cpp.sh/. Visual Studio…
3
votes
1 answer

Efficient way to iterate over each key exactly once in unordered_multimap

I have a std::unordered_multimap and I want to iterate over each key exactly once. What I'm currently doing is copying all keys into a std::set. This seems rather inefficient to me and I'm wondering if there is a smarter way of doing this. If it…
pekkas
  • 31
  • 5
2
votes
2 answers

finding all the values with given key for multimap

I am searching for all pairs for a particular key in a multimap using the code below. int main() { multimap mp; mp.insert({1,2}); mp.insert({11,22}); mp.insert({12,42}); mp.insert({1,2}); mp.insert({1,2}); for…
2
votes
1 answer

Is the a better way to do an average of a unordered_multimap key?

I'm searching for get all value of one key in my unordered_multimap, which is actually stacking an int and a value who represent an execution time in nanosecs. I need to get all value to replace the multi keys by one key with an average. I tried…
2
votes
1 answer

Error: static_assert failed "This hash only works for enumeration types" for unordered_multimap

I'm trying to insert into an unordered_multimap, int> like so: unordered_multimap, int> tree; auto firstPair=make_pair(firstNumber, secondNumber); tree.insert(make_pair(firstPair, 0)); However, the…
busebd12
  • 997
  • 2
  • 12
  • 24
2
votes
1 answer

Should items with duplicate keys in unordered_multimap be kept in the order of their insertion?

One book mentioned that for std::unordered_multimap: The order of the elements is undefined. The only guarantee is that duplicates, which are possible because a multiset is used, are grouped together in the order of their insertion. But from…
camino
  • 10,085
  • 20
  • 64
  • 115
1
vote
1 answer

Iterate over all elements with key k in a std::unordered_multimap in O(count(k))

I have a std::unordered_multimap, and would like to iterate over all elements with a given key k, without iterating the full map, but optimally traversing only the matching items. While I can do this with upper_bound in an ordered std::multimap, I…
galinette
  • 8,896
  • 2
  • 36
  • 87
1
vote
0 answers

Why are there so many buckets in c++11 unordered_multimap?

I have an unordered_multimap and I insert elements which have the same key many times, so some of them should go in the same bucket. I insert exactly 10000 elements. But for some reason when I print the bucket_count() it is 12983. This makes no…
1
vote
1 answer

Find function in STL C++ for unordered multimap?

The find function only returns one pointer for duplicate elements in multimap. How can we return pointer to other duplicate element in multimap.
gugle
  • 13
  • 2
1
vote
2 answers

Collisions in unordered_multimap when iterating through bucket via local_it

In the code below, I have a number of strings (DNA sequences) that I am storing in a vector. I have a struct, read_tag that I use to identify each string; read_tag.read_id is the string identifier. I take 30 character substrings of each string and…
izaak_pyzaak
  • 930
  • 8
  • 23
0
votes
0 answers

does find operation in multimap gives the lowest key value pair?

for example, if multimap is {{1,2}, {1,3}, {1,4},{2,3} }; will multimap.find(1) always return iterator pointing to {1,2} or it will return iterator pointing to any of the key-value pair with key = 1 ?
1
2