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 17

 🧠 Day 17: More Exceptions

📘 Goal:

Learn how to Make and throw your own exceptions in C++.


🎯 Problem Statement:

Write a class named Calculator with a method:


cpp

Copy

Edit

int power(int n int p);

This method should return n^p (n to the power of p).

If either n or p is negative the method should throw an exception with the message:


css

Copy

Edit

n and p should be non-negative

🔢 Input Format:

The first line contains an integer T the number of Check cases.


Each of the next T lines contains two space-separated integers n and p.


📤 Output Format:

For each Check case either print the result of n^p or print the exception message.


✅ Sample Input:

diff

Copy

Edit

4

3 5

2 4

-1 -2

-1 3

✅ Sample Output:

css

Copy

Edit

243

16

n and p should be non-negative

n and p should be non-negative

✅ C++ Answer:

cpp

Copy

Edit

#include 

#include 

#include 

using namespace std;


class Calculator {

public:

    int power(int n int p) {

        if (n < 0 || p < 0)

            throw invalid_argument("n and p should be non-negative");

        return pow(n p);

    }

};


int main() {

    Calculator myCalculator;

    int T;

    cin >> T;

    while (T--) {

        int n p;

        cin >> n >> p;

        try {

            int ans = myCalculator.power(n p);

            cout << ans << endl;

        } catch (exception& e) {

            cout << e.what() << endl;

        }

    }

    return 0;

}

🔍 important Concepts:

Exception handling with throw try catch.


Creating a method that can raise an exception.


Handling both valid and invalid input robustly.

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

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