Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ map erase(begin, end) not working
    primarykey
    data
    text
    <p>I'm trying to do my life easier by using the C++ API for map. Through the <code>map.erase(begin, end)</code> method I'm hoping to delete all the entries between <code>[begin, end)</code>. So my method is implemented and <code>TabletKey</code> are defined below.</p> <pre><code> 79 void 80 ObjectFinder::flush(uint64_t tableId) { 81 82 RAMCLOUD_TEST_LOG("flushing object map"); 83 std::map&lt;TabletKey, ProtoBuf::Tablets::Tablet&gt;::iterator lower; 84 std::map&lt;TabletKey, ProtoBuf::Tablets::Tablet&gt;::iterator upper; 85 std::map&lt;TabletKey, ProtoBuf::Tablets::Tablet&gt;::iterator it; 86 KeyHash keyHash = Key::getHash(tableId, "", 0); 87 TabletKey key(tableId, keyHash); 88 89 std::cout &lt;&lt; "before the loop" &lt;&lt; std::endl; 90 for (it = tableMap.begin(); it != tableMap.end(); it++) { 91 std::cout &lt;&lt; it-&gt;first.first &lt;&lt; std::endl; 92 } 93 lower = tableMap.lower_bound(key); 94 upper = tableMap.upper_bound(key); 95 108 tableMap.erase(lower, upper); 109 std::cout &lt;&lt; "After the erase" &lt;&lt; std::endl; 110 for (it = tableMap.begin(); it != tableMap.end(); it++) { 111 std::cout &lt;&lt; it-&gt;first.first &lt;&lt; std::endl; 112 } } </code></pre> <p>However, the <code>id</code> values are not being deleted:</p> <pre><code>id = 99 before the loop 1 99 After the erase 1 99 </code></pre> <p>I wrote my own <code>comparison</code> function, to overload the default method:</p> <pre><code> 35 typedef std::pair&lt;uint64_t, KeyHash&gt; TabletKey; 36 37 /* 38 * The object CmpTabletKey is used to override the default comparison 39 * definition from the C++ Map. 40 */ 41 struct CmpTabletKey { 42 bool operator()(const TabletKey&amp; key1, const TabletKey&amp; key2) const { 43 return ((key1.first &lt; key2.first) || 44 (key1.first == key2.first &amp;&amp; key1.second &lt; key2.second)); } } </code></pre> <p>Could someone give me a clue why is <code>erase</code> not working as expected? Do I have to give the definition of <code>CmpTabletKey</code> to the <code>iterator</code> as well? <strong><em>Update</em></strong> This is my old implementation: It works nicely, and do what I want: However, it's a O(n) method, and I want a faster implementation:</p> <pre><code>117 std::map&lt;TabletKey, ProtoBuf::Tablets::Tablet&gt;::iterator it; 118 for (it = tableMap.begin(); it != tableMap.end(); ) { 119 if (tableId == it-&gt;first.first) { 120 tableMap.erase((it++)-&gt;first); 121 } else { 122 ++it; 123 } 124 } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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