<![CDATA[Daz Blogs]]><![CDATA[Anything that I find interesting and worth sharing]]>https://dazzanova.hashnode.devhttps://cdn.hashnode.com/uploads/logos/6abc6e5dca2c510388bd0df8/f8018318-a193-4672-88fa-71a24c97262b.pngDaz Blogshttps://dazzanova.hashnode.devRSS for NodeWed, 02 Dec 2026 04:32:18 GMT<![CDATA[en]]>60<![CDATA[A Deep-dive on std::vector in C++ (Part 1)]]><![CDATA[There's a lot you may not know!]]>https://dazzanova.hashnode.dev/a-deep-dive-on-std-vector-in-c-part-1https://dazzanova.hashnode.dev/a-deep-dive-on-std-vector-in-c-part-1<![CDATA[#cpp #guide]]><![CDATA[C++]]><![CDATA[DSA]]><![CDATA[data structures]]><![CDATA[Anand Nair]]>Wed, 30 Sep 2026 04:03:45 GMT<![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 hood.

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.

How does it grow in size? What happens when they run out of space? What's the difference between size() and capacity() methods?

Let's take a chance of understanding it ->

I. Initialising a Vector

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.

II. When you ... don't?

Here comes the fun part:

When you write vector v;, the vector is initially empty. It has:

Size = 0

Capacity = 0

When you do:

v.push_back(10);

the vector needs to allocate memory to store the element. Now:

Size = 1

Capacity = 1

So does it stop here, since it can't allocate any more elements due to capacity limit?

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.

You can think of the capacity as doubling when the vector runs out of space. So now:

Size = 2

Capacity = 2

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:

Size = 3

Capacity = 4

And so on, the vector grows dynamically.

This happens to avoid repeated allocations, which can be expensive when dealing with a huge number of elements.

Here's a table for your reference:

Size 1 2 3 4 5

Capacity 1 2 4 4 8

The exact growth pattern depends on the implementation, but this doubling pattern is a useful way to understand the basic idea.

And that's the basic idea behind how a vector grows! What looks like a simple push_back() involves memory allocation and capacity management.

In the next blog, let's have a look at other features like the copy and move constructors and methods like reserve() and emplace_back().

  • Background image generated with the help of Chatgpt.
]]>