Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access/iterate over all non-unique keys in an unordered_multimap?
    text
    copied!<p>I would like to access/iterate over all non-unique keys in an unordered_multimap. The hash table basically is a map from a signature <code>&lt;SIG&gt;</code> that does indeed occur more than once in practice to identifiers <code>&lt;ID&gt;</code>. I would like to find those entries in the hash table where occurs once.</p> <p>Currently I use this approach:</p> <pre><code>// map &lt;SIG&gt; -&gt; &lt;ID&gt; typedef unordered_multimap&lt;int, int&gt; HashTable; HashTable&amp; ht = ...; for(HashTable::iterator it = ht.begin(); it != ht.end(); ++it) { size_t n=0; std::pair&lt;HashTable::iterator, HashTable::iterator&gt; itpair = ht.equal_range(it-&gt;first); for ( ; itpair.first != itpair.second; ++itpair.first) { ++n; } if( n &gt; 1 ){ // access those items again as the previous iterators are not valid anymore std::pair&lt;HashTable::iterator, HashTable::iterator&gt; itpair = ht.equal_range(it-&gt;first); for ( ; itpair.first != itpair.second; ++itpair.first) { // do something with those items } } } </code></pre> <p>This is certainly not efficient as the outer loop iterates over all elements of the hash table (via <code>ht.begin()</code>) and the inner loop tests if the corresponding key is present more than once.</p> <p>Is there a more efficient or elegant way to do this?</p> <p>Note: I know that with a <code>unordered_map</code> instead of <code>unordered_multimap</code> I wouldn't have this issue but due to application requirements I must be able to store multiple keys <code>&lt;SIG&gt;</code> pointing to different identifiers <code>&lt;ID&gt;</code>. Also, an <code>unordered_map&lt;SIG, vector&lt;ID&gt; &gt;</code> is not a good choice for me as it uses roughly 150% of memory as I have many unique keys and <code>vector&lt;ID&gt;</code> adds quite a bit of overhead for each item.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload