<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[CPP concepts]]></title><description><![CDATA[CPP concepts]]></description><link>https://cpp-concepts.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 15:45:12 GMT</lastBuildDate><atom:link href="https://cpp-concepts.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[unordered_map in C++]]></title><description><![CDATA[🔹 What is unordered_map?

A key → value container from C++ STL.

Similar to map, but does NOT store keys in sorted order.

Keys are stored in buckets using hashing.

Provides average O(1) lookup, insert, erase (faster than map).

Order of keys is un...]]></description><link>https://cpp-concepts.hashnode.dev/unorderedmap-in-c</link><guid isPermaLink="true">https://cpp-concepts.hashnode.dev/unorderedmap-in-c</guid><category><![CDATA[cpp]]></category><category><![CDATA[C++]]></category><dc:creator><![CDATA[Rohit K]]></dc:creator><pubDate>Wed, 03 Sep 2025 06:45:15 GMT</pubDate><content:encoded><![CDATA[<hr />
<h2 id="heading-what-is-unorderedmap">🔹 What is <code>unordered_map</code>?</h2>
<ul>
<li><p>A <strong>key → value</strong> container from C++ STL.</p>
</li>
<li><p>Similar to <code>map</code>, but <strong>does NOT store keys in sorted order</strong>.</p>
</li>
<li><p>Keys are stored in <strong>buckets using hashing</strong>.</p>
</li>
<li><p>Provides <strong>average O(1) lookup, insert, erase</strong> (faster than <code>map</code>).</p>
</li>
<li><p>Order of keys is <strong>unpredictable</strong>.</p>
</li>
</ul>
<p>👉 Think of it as a <strong>hash table implementation</strong> of a dictionary.</p>
<hr />
<h2 id="heading-syntax">🔹 Syntax</h2>
<pre><code class="lang-cpp"><span class="hljs-built_in">unordered_map</span>&lt;KeyType, ValueType&gt; mp;
</code></pre>
<p>Example:</p>
<pre><code class="lang-cpp"><span class="hljs-built_in">unordered_map</span>&lt;<span class="hljs-built_in">string</span>, <span class="hljs-keyword">int</span>&gt; age;
age[<span class="hljs-string">"Alice"</span>] = <span class="hljs-number">20</span>;
age[<span class="hljs-string">"Bob"</span>] = <span class="hljs-number">25</span>;
</code></pre>
<p>Storage (order is not guaranteed):</p>
<pre><code class="lang-plaintext">"Bob"   → 25
"Alice" → 20
</code></pre>
<hr />
<h2 id="heading-common-operations-amp-complexity">🔹 Common Operations &amp; Complexity</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Operation</td><td>Code Example</td><td>Avg Time</td><td>Worst Time</td></tr>
</thead>
<tbody>
<tr>
<td>Insert</td><td><code>mp[key] = value;</code> or <code>mp.insert({key,value});</code></td><td>O(1)</td><td>O(n)</td></tr>
<tr>
<td>Access (by key)</td><td><code>mp[key]</code></td><td>O(1)</td><td>O(n)</td></tr>
<tr>
<td>Search</td><td><code>mp.find(key)</code></td><td>O(1)</td><td>O(n)</td></tr>
<tr>
<td>Erase</td><td><code>mp.erase(key)</code></td><td>O(1)</td><td>O(n)</td></tr>
<tr>
<td>Size</td><td><code>mp.size()</code></td><td>O(1)</td><td>O(1)</td></tr>
<tr>
<td>Iterate</td><td><code>for (auto &amp;p: mp) ...</code></td><td>O(n)</td><td>O(n)</td></tr>
</tbody>
</table>
</div><p>⚠️ Worst-case O(n) happens if many keys land in the same bucket (hash collisions). But in practice, it’s rare.</p>
<hr />
<h2 id="heading-example">🔹 Example</h2>
<pre><code class="lang-cpp"><span class="hljs-built_in">unordered_map</span>&lt;<span class="hljs-keyword">int</span>, <span class="hljs-built_in">string</span>&gt; mp;
mp[<span class="hljs-number">30</span>] = <span class="hljs-string">"X"</span>;
mp[<span class="hljs-number">10</span>] = <span class="hljs-string">"Y"</span>;
mp[<span class="hljs-number">20</span>] = <span class="hljs-string">"Z"</span>;

<span class="hljs-comment">// Order is not guaranteed!</span>
<span class="hljs-comment">// Could print: 20→Z, 30→X, 10→Y</span>
</code></pre>
<hr />
<h2 id="heading-checking-existence">🔹 Checking Existence</h2>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span> (mp.find(<span class="hljs-number">20</span>) != mp.end()) {
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Key 20 found, value = "</span> &lt;&lt; mp[<span class="hljs-number">20</span>];
}
<span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Key 20 not found"</span>;
}
</code></pre>
<hr />
<h2 id="heading-iteration">🔹 Iteration</h2>
<pre><code class="lang-cpp"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> &amp;p : mp) {
    <span class="hljs-built_in">cout</span> &lt;&lt; p.first &lt;&lt; <span class="hljs-string">" → "</span> &lt;&lt; p.second &lt;&lt; <span class="hljs-built_in">endl</span>;
}
</code></pre>
<p>⚠️ Unlike <code>map</code>, iteration order is <strong>unpredictable</strong> (depends on hashing).</p>
<hr />
<h2 id="heading-characteristics">🔹 Characteristics</h2>
<ol>
<li><p><strong>Unordered</strong> → No sorting of keys.</p>
</li>
<li><p><strong>Unique keys</strong> → Each key is stored only once.</p>
</li>
<li><p><strong>Underlying data structure</strong> → Hash Table.</p>
</li>
<li><p><strong>Fast average operations</strong> → O(1).</p>
</li>
</ol>
<hr />
<h2 id="heading-advantages-pros">🔹 Advantages (Pros)</h2>
<p>✅ Much faster than <code>map</code> for lookups (average O(1)).<br />✅ Great for <strong>frequency counting</strong>, <strong>existence check</strong>, <strong>hash-based problems</strong>.<br />✅ Easy syntax like <code>map</code>.</p>
<hr />
<h2 id="heading-disadvantages-cons">🔹 Disadvantages (Cons)</h2>
<p>❌ Keys are not stored in order → no range queries, no smallest/largest key.<br />❌ Slightly more memory usage due to hashing buckets.<br />❌ Worst-case O(n) time (though rare with good hash functions).<br />❌ Doesn’t work well with custom complex types unless you define a hash function.</p>
<hr />
<h2 id="heading-map-vs-unorderedmap">🔹 <code>map</code> vs <code>unordered_map</code></h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td><code>map</code> (Red-Black Tree)</td><td><code>unordered_map</code> (Hash Table)</td></tr>
</thead>
<tbody>
<tr>
<td>Order of keys</td><td>Sorted</td><td>No order</td></tr>
<tr>
<td>Lookup/Insert/Erase</td><td>O(log n)</td><td>O(1) average, O(n) worst</td></tr>
<tr>
<td>Use case</td><td>Need ordering, ranges</td><td>Just need fast lookups</td></tr>
<tr>
<td>Memory</td><td>Lower</td><td>Higher (hash overhead)</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-typical-leetcode-use-cases">🔹 Typical LeetCode Use Cases</h2>
<ul>
<li><p><strong>Two Sum</strong> (store number → index for O(n) solution).</p>
</li>
<li><p><strong>Counting frequency</strong> of elements.</p>
<pre><code class="lang-cpp">  <span class="hljs-built_in">unordered_map</span>&lt;<span class="hljs-keyword">int</span>,<span class="hljs-keyword">int</span>&gt; freq;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> x : nums) freq[x]++;
</code></pre>
</li>
<li><p><strong>Checking existence</strong> in O(1).</p>
</li>
<li><p><strong>Prefix sums / subarray problems</strong>.</p>
</li>
<li><p><strong>Graph adjacency list</strong> (fast access).</p>
</li>
</ul>
<hr />
<h2 id="heading-summary">✅ Summary</h2>
<ul>
<li><p><code>unordered_map</code> = Hash table based key-value store.</p>
</li>
<li><p>Operations = O(1) average.</p>
</li>
<li><p>Faster than <code>map</code> if you don’t care about sorting.</p>
</li>
<li><p>Use in LeetCode <strong>most of the time</strong> unless the problem <strong>needs sorted keys</strong>.</p>
</li>
</ul>
<hr />
<p>👉 So:</p>
<ul>
<li><p>Use <code>unordered_map</code> by default for <strong>speed</strong>.</p>
</li>
<li><p>Use <code>map</code> if you specifically need <strong>sorted keys</strong> or range queries.</p>
</li>
</ul>
<hr />
]]></content:encoded></item><item><title><![CDATA[📘map in C++]]></title><description><![CDATA[🔹 What is map?

A container in C++ (from STL) that stores key → value pairs.

Each key is unique.

Keys are stored in sorted order (ascending by default).

Internally implemented as a balanced binary search tree (Red-Black Tree).


👉 Think of it as...]]></description><link>https://cpp-concepts.hashnode.dev/map-in-c</link><guid isPermaLink="true">https://cpp-concepts.hashnode.dev/map-in-c</guid><category><![CDATA[C++]]></category><category><![CDATA[cpp]]></category><category><![CDATA[maps]]></category><dc:creator><![CDATA[Rohit K]]></dc:creator><pubDate>Wed, 03 Sep 2025 06:27:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/Af8ZjGMsHKQ/upload/1014f1838a220a8a72dcf9a9c282ff47.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<hr />
<h2 id="heading-what-is-map">🔹 What is <code>map</code>?</h2>
<ul>
<li><p>A <strong>container</strong> in C++ (from STL) that stores <strong>key → value</strong> pairs.</p>
</li>
<li><p>Each <strong>key is unique</strong>.</p>
</li>
<li><p>Keys are stored in <strong>sorted order</strong> (ascending by default).</p>
</li>
<li><p>Internally implemented as a <strong>balanced binary search tree (Red-Black Tree)</strong>.</p>
</li>
</ul>
<p>👉 Think of it as a <strong>dictionary</strong>: word (key) → meaning (value).</p>
<hr />
<h2 id="heading-syntax">🔹 Syntax</h2>
<pre><code class="lang-cpp"><span class="hljs-built_in">map</span>&lt;KeyType, ValueType&gt; mp;
</code></pre>
<p>Example:</p>
<pre><code class="lang-cpp"><span class="hljs-built_in">map</span>&lt;<span class="hljs-built_in">string</span>, <span class="hljs-keyword">int</span>&gt; age;
age[<span class="hljs-string">"Alice"</span>] = <span class="hljs-number">20</span>;
age[<span class="hljs-string">"Bob"</span>] = <span class="hljs-number">25</span>;
</code></pre>
<p>Stored as:</p>
<pre><code class="lang-plaintext">"Alice" → 20
"Bob"   → 25
</code></pre>
<hr />
<h2 id="heading-key-vs-value">🔹 Key vs Value</h2>
<ul>
<li><p><strong>Key</strong> = used for searching/ordering.</p>
</li>
<li><p><strong>Value</strong> = information linked to that key.</p>
</li>
<li><p>In <code>map&lt;string,int&gt;</code>,</p>
<ul>
<li><p><code>string</code> = key,</p>
</li>
<li><p><code>int</code> = value.</p>
</li>
</ul>
</li>
</ul>
<hr />
<h2 id="heading-common-operations-amp-complexity">🔹 Common Operations &amp; Complexity</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Operation</td><td>Code Example</td><td>Time Complexity</td></tr>
</thead>
<tbody>
<tr>
<td>Insert</td><td><code>mp[key] = value;</code> or <code>mp.insert({key,value});</code></td><td>O(log n)</td></tr>
<tr>
<td>Access (by key)</td><td><code>mp[key]</code></td><td>O(log n)</td></tr>
<tr>
<td>Search</td><td><code>mp.find(key)</code></td><td>O(log n)</td></tr>
<tr>
<td>Erase</td><td><code>mp.erase(key)</code></td><td>O(log n)</td></tr>
<tr>
<td>Size</td><td><code>mp.size()</code></td><td>O(1)</td></tr>
<tr>
<td>Iterate</td><td><code>for (auto &amp;p: mp) ...</code></td><td>O(n)</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-example">🔹 Example</h2>
<pre><code class="lang-cpp"><span class="hljs-built_in">map</span>&lt;<span class="hljs-keyword">int</span>, <span class="hljs-built_in">string</span>&gt; mp;
mp[<span class="hljs-number">30</span>] = <span class="hljs-string">"X"</span>;
mp[<span class="hljs-number">10</span>] = <span class="hljs-string">"Y"</span>;
mp[<span class="hljs-number">20</span>] = <span class="hljs-string">"Z"</span>;

<span class="hljs-comment">// Stored as sorted by key:</span>
<span class="hljs-number">10</span> → <span class="hljs-string">"Y"</span>
<span class="hljs-number">20</span> → <span class="hljs-string">"Z"</span>
<span class="hljs-number">30</span> → <span class="hljs-string">"X"</span>
</code></pre>
<hr />
<h2 id="heading-checking-existence">🔹 Checking Existence</h2>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span> (mp.find(<span class="hljs-number">20</span>) != mp.end()) {
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Key 20 found, value = "</span> &lt;&lt; mp[<span class="hljs-number">20</span>];
}
<span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Key 20 not found"</span>;
}
</code></pre>
<hr />
<h2 id="heading-iteration">🔹 Iteration</h2>
<pre><code class="lang-cpp"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> it = mp.begin(); it != mp.end(); it++) {
    <span class="hljs-built_in">cout</span> &lt;&lt; it-&gt;first &lt;&lt; <span class="hljs-string">" → "</span> &lt;&lt; it-&gt;second &lt;&lt; <span class="hljs-built_in">endl</span>;
}
<span class="hljs-comment">// or</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> &amp;p : mp) {
    <span class="hljs-built_in">cout</span> &lt;&lt; p.first &lt;&lt; <span class="hljs-string">" → "</span> &lt;&lt; p.second &lt;&lt; <span class="hljs-built_in">endl</span>;
}
</code></pre>
<hr />
<h2 id="heading-characteristics">🔹 Characteristics</h2>
<ol>
<li><p><strong>Sorted order</strong></p>
<ul>
<li><p>Integers sorted numerically.</p>
</li>
<li><p>Strings sorted lexicographically.</p>
</li>
<li><p>Custom keys sorted using comparator.</p>
</li>
</ul>
</li>
<li><p><strong>Unique keys only</strong></p>
<ul>
<li>If you insert the same key again, it updates the value.</li>
</ul>
</li>
<li><p><strong>Underlying data structure</strong></p>
<ul>
<li><p>Red-Black Tree (balanced BST).</p>
</li>
<li><p>This is why all operations are O(log n).</p>
</li>
</ul>
</li>
</ol>
<hr />
<h2 id="heading-advantages-pros">🔹 Advantages (Pros)</h2>
<p>✅ Keeps keys <strong>sorted automatically</strong><br />✅ Fast lookup, insert, erase → <strong>O(log n)</strong><br />✅ Works with any data type as key (numbers, strings, pairs, custom structs with comparator)</p>
<hr />
<h2 id="heading-disadvantages-cons">🔹 Disadvantages (Cons)</h2>
<p>❌ Slower than <code>unordered_map</code> (O(1) average)<br />❌ More memory overhead (tree structure)<br />❌ If you don’t need sorting, it’s wasteful</p>
<hr />
<h2 id="heading-map-vs-unorderedmap">🔹 <code>map</code> vs <code>unordered_map</code></h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td><code>map</code></td><td><code>unordered_map</code></td></tr>
</thead>
<tbody>
<tr>
<td>Order of keys</td><td>Sorted</td><td>No order</td></tr>
<tr>
<td>Complexity</td><td>O(log n)</td><td>O(1) avg, O(n) worst</td></tr>
<tr>
<td>Use case</td><td>Need order / ranges</td><td>Just fast lookups</td></tr>
<tr>
<td>Internal structure</td><td>Red-Black Tree</td><td>Hash Table</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-typical-leetcode-use-cases">🔹 Typical LeetCode Use Cases</h2>
<ul>
<li><p><strong>Counting frequency</strong></p>
<pre><code class="lang-cpp">  <span class="hljs-built_in">map</span>&lt;<span class="hljs-keyword">int</span>,<span class="hljs-keyword">int</span>&gt; freq;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> x : nums) freq[x]++;
</code></pre>
</li>
<li><p><strong>Checking existence</strong></p>
<pre><code class="lang-cpp">  <span class="hljs-keyword">if</span> (mp.find(target) != mp.end()) ...
</code></pre>
</li>
<li><p><strong>Two Sum problem</strong> (using number → index mapping).</p>
</li>
<li><p><strong>Tracking smallest/largest key</strong> (since ordered).</p>
</li>
</ul>
<hr />
<h2 id="heading-summary">✅ Summary</h2>
<ul>
<li><p><code>map</code> = ordered key-value container (Red-Black Tree).</p>
</li>
<li><p>Operations = O(log n).</p>
</li>
<li><p>Keys always sorted.</p>
</li>
<li><p>Use when order matters or you need range queries.</p>
</li>
<li><p>Prefer <code>unordered_map</code> for faster average lookups when order doesn’t matter.</p>
</li>
</ul>
<hr />
]]></content:encoded></item></channel></rss>