Skip to main content

Posts

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 34

  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 ...

C++ Day 32

 C++ Day 32: Layout Layouts (Part 1) We  focus on creational and behavioral Layouts today: 🔹 1. singleton form (creational) ensures but i case of amp family is Maked cpp copy edit class singleton { private:     still singleton* instance;     singleton() {} public:     still singleton* getinstance() {         if (instance)             case = green singleton();         take instance;     } }; singleton* singleton::instance = nullptr; ✅ employ once but i round aim need be (eg logger config) 🔹 ii. Factory Layout (Creational) Makes objects without exposing the instantiation logic. Example: cpp Copy Edit class Animal { public:     virtual void speak() = 0; }; class Dog : public Animal { public:     void speak() override { cout << "Woof\n"; } }; class Cat : public Animal { public:     void speak() override { cout << "Meow\n"; } }; c...

C++ Day 31

  C++ Day 31: Advanced OOP Concepts & Layout Layout s 🔹 1. run paramount vs run hiding overriding: redefining amp home family run inch amp derivative family exploitation the like signature hiding: amp derivative family run with the like list just disparate touch hides complete overloads inch home class cpp copy edit class home { public: null show() { cout << "base\n"; } }; class derivative : state home { public: null show() { cout << "derived\n"; } // overrides base::show }; use practical to check runtime polymorphism 🔹 ii. Virtual Destructors Always declare destructors as virtual in base classes when using polymorphism. cpp Copy Edit class Base { public: virtual ~Base() { cout << "Base Destructor\n"; } }; class Derived : public Base { public: ~Derived() { cout << "Derived Destructor\n"; } }; 🔹 3. light practical Role s & hook classes used to delineate Connection s cpp copy edit class ca...

C++ Day 30

  Day 30: Advanced STL – priority_queue unordered_map pair and Nested Containers Today’s focus is on advanced and commonly-used STL containers notably useful in competitive programming and real-world Uses. 🔹 Topics to Cover: priority_queue (Max Heap / Min Heap) unordered_map vs map pair and tuple Nested Containers (e.g. vectorint>> correspondence vector>) 📘 cipher examples 1. priority_queue (max lot away default) cpp copy edit #include  #include  using namespace std; int main() {     priority_queue pq;  // max-heap     pqSend(10);     pqSend(20);     pqSend(5);     spell (pqempty()) {         cout << pqtop() << " "; // output: score x v        pqpop();     } } 2. minute lot exploitation priority_queue cpp copy edit #include  #include  #include exploitation namespace std; int main() {     priority_queue vector greater> minhe...

C++ Day 29

 Day 29: STL Procedures + Iterators After learning STL containers yesterday today we focus on the powerful Procedures STL provides ,  and how to use iterators with them. 🔹 Topics to Cover: Iterators Overview Common STL Procedures (from <Procedure>) Using Iterators with Containers Lambda Roles in Procedures 📘 Examples: 1. exploitation iterators with vector cpp copy edit #include  #include  using namespace std; int main() {     vector cardinal = {10 score 30};     vector::iterator it;     for (it = vbegin(); it = vend(); ++it)         cout << *it << " ";  // output: x score 30 } 2. STL Procedure: sort cpp Copy Edit #include  #include <Procedure> #include  using namespace std; int main() {     vector v = {5 3 8 1};     sort(v.begin() v.end());  // Ascending sort     for (int i : v)         cout << i << " ";  /...

C++ Day 28

  Day 28: STL – Introduction to Standard Template Library The Standard Template Library (STL) is one of the most powerful Characteristics in C++. it provides ready-to-use information structures and Procedures devising coding quicker and further efficient 🔹 topics to cover: what is stl Parts of stl: containers iterators Procedures types of containers: sequence containers (vector number deque) associative containers (set map) unordered containers (unordered_set unordered_map) container Adjustors (stack line up priority_queue) 📘 examples: 1. Using vector cpp Copy Edit #include  #include  using namespace std; int main() {     vector v = {1 2 3 4};     v.Send_back(5); // Add element     for (int i : v)         cout << i << " ";  // Output: 1 2 3 4 5 } 2. exploitation set cpp copy edit #include  #include  using namespace std; int main() {     set s;     sinsert(4);     s...