<?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[Daz Blogs]]></title><description><![CDATA[Anything that I find interesting and worth sharing]]></description><link>https://dazzanova.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/6abc6e5dca2c510388bd0df8/f8018318-a193-4672-88fa-71a24c97262b.png</url><title>Daz Blogs</title><link>https://dazzanova.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Sun, 11 Oct 2026 10:31:22 GMT</lastBuildDate><atom:link href="https://dazzanova.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[A Deep-dive on std::vector in C++ (Part 1)]]></title><description><![CDATA[I have been learning Data structures and Algorithms for online assessment rounds, primarily in C++. One day I randomly decided to take a turn towards understanding how C++ actually works under the hoo]]></description><link>https://dazzanova.hashnode.dev/a-deep-dive-on-std-vector-in-c-part-1</link><guid isPermaLink="true">https://dazzanova.hashnode.dev/a-deep-dive-on-std-vector-in-c-part-1</guid><category><![CDATA[#cpp #guide]]></category><category><![CDATA[C++]]></category><category><![CDATA[DSA]]></category><category><![CDATA[data structures]]></category><dc:creator><![CDATA[Anand Nair]]></dc:creator><pubDate>Wed, 30 Sep 2026 04:03:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6abc6e5dca2c510388bd0df8/7ba86bcb-8f63-4eec-9a5b-b5f52418fa63.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I have been learning Data structures and Algorithms for online assessment rounds, primarily in C++. One day I randomly decided to take a turn towards understanding how C++ actually works under the hood.</p>
<p>And while discussing Data structures , what's better than discussing the fan favourite - Vectors! We most probably have studied Vectors as “containers that store elements in contiguous memory and can dynamically grow or shrink in size”. That definition is correct, but it's just the top of the iceberg.</p>
<p>How does it grow in size? What happens when they run out of space? What's the difference between <strong>size()</strong> and <strong>capacity()</strong> methods?</p>
<p>Let's take a chance of understanding it -&gt;</p>
<h3><strong>I. Initialising a Vector</strong></h3>
<p>When you initialize a vector with a fixed size, let's say "n", then voila, you have an Array! You can work with it like any other array, writing and accessing elements by index and even rewriting them. And yeah, a vector is zero indexed too.</p>
<h3><strong>II. When you ... don't?</strong></h3>
<p>Here comes the fun part:</p>
<p>When you write <em>vector v;</em>, the vector is initially empty. It has:</p>
<p>Size = 0</p>
<p>Capacity = 0</p>
<p>When you do:</p>
<p><em>v.push_back(10)</em>;</p>
<p>the vector needs to allocate memory to store the element. Now:</p>
<p>Size = 1</p>
<p>Capacity = 1</p>
<p>So does it stop here, since it can't allocate any more elements due to capacity limit?</p>
<p>It could have, but the Vector is smart. When you try to insert another element and the current capacity isn't enough, it allocates a larger block of memory.</p>
<p>You can think of the capacity as doubling when the vector runs out of space. So now:</p>
<p>Size = 2</p>
<p>Capacity = 2</p>
<p>After performing a push back again, it reaches the limit. When another element is inserted, the vector again needs more space, so its capacity grows further:</p>
<p>Size = 3</p>
<p>Capacity = 4</p>
<p>And so on, the vector grows dynamically.</p>
<p>This happens to avoid repeated allocations, which can be expensive when dealing with a huge number of elements.</p>
<p>Here's a table for your reference:</p>
<h3><strong>Size 1 2 3 4 5</strong></h3>
<h3><strong>Capacity 1 2 4 4 8</strong></h3>
<p>The exact growth pattern depends on the implementation, but this doubling pattern is a useful way to understand the basic idea.</p>
<p>And that's the basic idea behind how a vector grows! What looks like a simple push_back() involves memory allocation and capacity management.</p>
<p>In the next blog, let's have a look at other features like the copy and move constructors and methods like <strong>reserve()</strong> and <strong>emplace_back().</strong></p>
<ul>
<li>Background image generated with the help of Chatgpt.</li>
</ul>
]]></content:encoded></item></channel></rss>