๐
Date: Feb 6, 2026
๐ฅ Topic: STL, Vector Basics, Capacity vs Size
๐ฆ What is STL?
STL (Standard Template Library) is a collection of pre-written algorithms and data structures. Vector is the most used container in STL.
๐ Vector: The Dynamic Array
A Vector is an array that grows automatically.
When it gets full, it doubles its capacity!
Key Functions:
v.push_back(x): Adds element to end.v.pop_back(): Removes last element.v.size(): How many elements are currently stored.v.capacity(): How much space is actually reserved.
๐ป Day 36 Code: Vector Playground
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Initialization
vector<int> v = {1, 2, 3};
cout << "Capacity: " << v.capacity() << endl;
cout << "Size: " << v.size() << endl;
// Adding element
v.push_back(4);
cout << "New Capacity: " << v.capacity() << endl; // Likely doubles to 6
cout << "New Size: " << v.size() << endl;
return 0;
}
๐ญ Thoughts
The magic of capacity doubling is efficient but costs memory. Using vector feels like cheating because it's so easy compared to arrays!
No comments:
Post a Comment