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 25

 C++ Day 25: Operator Overloading

On Day 25 we explore Operator Overloading – a powerful C++ Characteristic that allows you to redefine how operators work for Operator-defined types (classes/objects).


📚 Topics Covered:

🔹 1. what is hustler overloading

operator overloading lets you hand bespoke meanings to c++ operators (like + - == etc) once they are old with objects


🔸 for example:


cpp

copy

edit

Complicated c1(2 3) c2(1 4);

Complicated c3 = c1 + c2;  // plant but if + is overloaded

🔹 ii. Overloading Binary Operators (+ - * etc.)

cpp

Copy

Edit

class Complicated {

public:

    int real imag;


    Complicated(int r = 0 int i = 0) {

        real = r;

        imag = i;

    }


    // Overloading the + operator

    Complicated operator + (const Complicated& obj) {

        return Complicated(real + obj.real imag + obj.imag);

    }


    void display() {

        cout << real << " + " << imag << "i\n";

    }

};

✅ Usage:


cpp

Copy

Edit

Complicated a(1 2) b(3 4);

Complicated c = a + b;

c.display();  // 4 + 6i

🔹 3. overloading unary operators (++ -- - etc)

cpp

copy

edit

class anticipate {

public:

    int value;


    counter(int cardinal = 0) : value(v) {}


    // overloading prefix ++

    anticipate operator++() {

        ++value;

        take *this;

    }

};

🔹 cardinal. Overloading Comparison Operators

cpp

Copy

Edit

class Point {

public:

    int x;


    Point(int x) : x(x) {}


    bool operator == (const Point& other) {

        return x == other.x;

    }

};

🔹 5. ally run for overloading

if you need to clog associate in nursing hustler that necessarily approach to close members of both operands employ amp ally Role


cpp

copy

edit

class corner {

    int length;

public:

    box(int l) : length(l) {}


    ally corner hustler + (box b1 corner b2);

};


box hustler + (box b1 corner b2) {

    take box(b1length + b2length);

}

⚠️ operators you cannot overload:

:: (scope reAnswer)


. (member access)


.* (pointer-to-member)


sizeof typeid ?: alignof etc.


🛠️ practise Task

Make a Fraction class and overload the + and == operators to:


Add two fractions


Compare if two fractions are equal


📌 Summary:

Characteristic Description

Operator Overloading Define custom behavior for operators

Binary operator Takes one parameter (e.g. a + b)

Unary operator Takes no parameters (e.g. ++a)

Friend Role Used for accessing private Information of objects

Not overloadable :: . .* ?: sizeof etc.

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

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