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 12: Dynamic memory and pointers in OOP
🔹 1. Dynamic memory with new and removal New: Dynamically allocates memory Remove: Dealing Memory (important to prevent memory leaks) CPP Copy edit Int* p = new int (5); // Dynamically allocates memory cout << *p << endl; // print 5 Remove P; // Deals Memory 🔹 2. Dynamic arrays CPP Copy edit Int* Arr = new int [5]; // 5 Allocates memory for integer For (int i = 0; i <5; ++ i) { Arr [i] = i * 10; , Remove [] Arrested; // Removes the entire array 🔹 3. Classes with dynamic memory ❗ It matters: When your class makes new uses to allocate memory (eg, dynamic string or array), you have to define: Copy manufacturer Destroyer Assignment operator (rule of three) 🔹 4. Example: square with dynamic memory CPP Copy edit #INCLUDE <iostream> #INCLUDE <CSTRING> using namespace std; Class student { Personal: char name; public: Student (Const char* n) { Name = new four [strlen (n) + 1]; Strcpy (name, n); , // Copy Constructor (Deep Copy) Student (cast student and others) { Name = new four [Strlen (Ond.Name) + 1]; Strcpy (name, other. Name); , ~ Student () { Remove [] name; , Zero display () { Cout << "Name:" << Name << Endl; , , int main() { Students S1 ("Alice"); Student S2 = S1; // Deep Copy s1.display (); s2.display (); Return 0; , 🔹 5. Signs for goods CPP Copy edit Class car { public: Zero drive () { cout << "driving the car" << Endl; , , int main() { Car* Carptra = new car (); Carptr-> drive (); // Use -> With signal Remove Carptr; Return 0; , 🧠 practice practice Write a square person with a dynamically allotted four* name. Add: Manufacturer Copy manufacturer Destruction
Comments
Post a Comment