Skip to main content

C++ Day 39

  C++ Day 39 STL Containers (Deep Understanding & Real Usage) Till now, you already know arrays, vectors, loops, and STL algorithms. Today, we go one step deeper and understand STL containers , which are the backbone of modern C++ programming. In real projects and competitive coding, choice of container matters a lot. 1. What are STL Containers? STL containers are data structures provided by C++ to store data efficiently. They handle: memory management resizing element access performance optimization You focus on logic , not memory handling. 2. Categories of STL Containers STL containers are mainly divided into: Sequence Containers Associative Containers Unordered Containers Container Adapters 3. Sequence Containers These store data in sequence . 3.1 Vector Most used container in C++. vector< int > v; Key Features: Dynamic size Contiguous memory Fast random access Slower insertion in middle Example: v. push_...

C++ Day 39

 

C++ Day 39

STL Containers (Deep Understanding & Real Usage)

Till now, you already know arrays, vectors, loops, and STL algorithms.
Today, we go one step deeper and understand STL containers, which are the backbone of modern C++ programming.

In real projects and competitive coding, choice of container matters a lot.


1. What are STL Containers?

STL containers are data structures provided by C++ to store data efficiently.

They handle:

  • memory management

  • resizing

  • element access

  • performance optimization

You focus on logic, not memory handling.


2. Categories of STL Containers

STL containers are mainly divided into:

  1. Sequence Containers

  2. Associative Containers

  3. Unordered Containers

  4. Container Adapters


3. Sequence Containers

These store data in sequence.

3.1 Vector

Most used container in C++.

vector<int> v;

Key Features:

  • Dynamic size

  • Contiguous memory

  • Fast random access

  • Slower insertion in middle

Example:

v.push_back(10); v.push_back(20); v.push_back(30);

When to Use Vector:

  • When index access is needed

  • When size changes frequently

  • Competitive programming


3.2 Deque (Double Ended Queue)

deque<int> dq;

Features:

  • Fast insertion at front and back

  • Random access supported

  • Slightly slower than vector

dq.push_front(5); dq.push_back(10);

3.3 List (Doubly Linked List)

list<int> lst;

Features:

  • Non-contiguous memory

  • Fast insertion and deletion anywhere

  • No random access

lst.push_back(1); lst.push_front(0);

Use when frequent insert/delete in middle is required.


4. Associative Containers (Ordered)

These store data in sorted order.

4.1 Set

set<int> s;

Properties:

  • Stores unique elements

  • Sorted automatically

  • Implemented using Red-Black Tree

s.insert(5); s.insert(1); s.insert(5); // ignored

4.2 Multiset

multiset<int> ms;

Allows duplicate values.

ms.insert(5); ms.insert(5);

4.3 Map

Stores key-value pairs.

map<int, string> mp;

Example:

mp[1] = "Apple"; mp[2] = "Banana";
  • Keys are unique

  • Automatically sorted by key


5. Unordered Containers

These use hashing.

5.1 Unordered Set

unordered_set<int> us;
  • Unique elements

  • No order

  • Faster than set in most cases


5.2 Unordered Map

unordered_map<int, int> ump;

Used heavily in frequency counting.

ump[x]++;

Average complexity is O(1).


6. Container Adapters

These provide restricted access.

6.1 Stack

stack<int> st;

LIFO (Last In First Out)

st.push(10); st.pop();

6.2 Queue

queue<int> q;

FIFO (First In First Out)


6.3 Priority Queue

priority_queue<int> pq;
  • Max heap by default

pq.push(5); pq.push(10);

For min heap:

priority_queue<int, vector<int>, greater<int>> pq;

7. Choosing the Right Container

RequirementContainer
Fast access by indexvector
Insert/delete in middlelist
Unique sorted dataset
Key-value pairmap
Fast lookupunordered_map
Max/Min elementpriority_queue

8. Real-World Example

Counting frequency of numbers:

unordered_map<int, int> freq; for(int x : v) freq[x]++;

Used in:

  • interview problems

  • data analysis

  • competitive coding


9. Common Mistakes

  • Using map instead of unordered_map unnecessarily

  • Using vector when frequent middle insertion is needed

  • Forgetting that set/map are sorted

  • Expecting index access in list or set


10. Summary (Day 39)

Today you learned:

  • Types of STL containers

  • Differences between them

  • When to use which container

  • Real-world usage patterns

  • Performance considerations

Comments

Popular posts from this blog

C++ Day 35

  C++ Day 34: Layout Layouts (Part 2) We’ll cover: Constructer Layout Adjuster Layout Decorator Layout practise Task 🔹 1. developer form (creational) used to make compound objects measure away step ✅ employ case: you need to form associate in nursing aim (like amp pizza pie calculator house) with elective parameters example: cpp copy edit class calculator {     train Methodor gpu ram; public:     family developer {         train Methodor gpu ram;     public:         developer setcpu(string c) { Methodor = c; take *this; }         developer setgpu(string g) { gpu = g; take *this; }         developer setram(string r) { run = r; take *this; }         calculator Construct() {             take Calculater(cpu gpu ram);         }     };     Calculater(string snow train m train r) : cpu(c) gp...

C++ Day 33

  C++ Day 33: Smart Pointers & Memory Management 🔹 1. wherefore forward pointers in c++ hand-operated green / cancel is error-prone: memory leaks 🧠 double deletes ❌ dangling pointers 💥 smart pointers care store mechanically exploitation raii (Supply skill is initialization) 🔹 ii. Types of Smart Pointers in C++ ✅ std::unique_ptr Sole ownership of a Supply. Cannot be copied. Automatically deletes the Supply when it goes out of scope. cpp Copy Edit #include  unique_ptr ptr1 = make_unique(10); cout << *ptr1 << endl; // 10 You can transfer ownership: cpp Copy Edit unique_ptr ptr2 = move(ptr1); ✅ std::shared_ptr Shared ownership multiple shared_ptrs can point to the same object. Uses reference counting to track how many owners. cpp Copy Edit shared_ptr p1 = make_shared(100); shared_ptr p2 = p1;  // Reference count = 2 When count goes to 0 memory is released. ✅ std::weak_ptr Non-owning reference to a shared_ptr-managed object. Used to break cyclic references ...

CSES Increasing Subsequence solution

 You are given an array containing  n n n integers. Your task is to determine the longest increasing subsequence in the array, i.e., the longest subsequence where every element is larger than the previous one. A subsequence is a sequence that can be derived from the array by deleting some elements without changing the order of the remaining elements. Input The first line contains an integer n n n : the size of the array. After this there are n n n integers x 1 , x 2 , … , x n x_1,x_2,\ldots,x_n x 1 ​ , x 2 ​ , … , x n ​ : the contents of the array. Output Print the length of the longest increasing subsequence. Constraints 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1 ≤ n ≤ 2 ⋅ 1 0 5 1 ≤ x i ≤ 1 0 9 1 \le x_i \le 10^9 1 ≤ x i ​ ≤ 1 0 9 Example Input: 8 7 3 5 3 6 2 9 8 Output: 4 #include < bits / stdc ++. h > using namespace std ; void solve (){ int n ; cin >> n ; vector <int> arr ( n ); for ( int i = 0 ; i < n ; i ++)...