C++ Articles

Page 190 of 597

Customizing termination behavior for uncaught exception In C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 398 Views

In this tutorial, we will be discussing a program to customize behavior for an uncaught exceptions in C++.Usually, the exception is handled by the try-catch block, but there are instances where there isn’t a matching catch block and the program just terminates. This terminate() function is modifiable as per user requirements.Example#include #include using namespace std; //defining custom terminator void myhandler(){    cout

Read More

Remove Covered Intervals in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 377 Views

Suppose we have a list of intervals, we have to remove all intervals that are covered by another interval in the list. Here Interval [a,b) is covered by interval [c,d) if and only if c

Read More

Different methods to reverse a string in C/C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 3K+ Views

In this tutorial, we will be discussing a program to understand different methods to reverse a string in C/C++.ExampleUser-defined reverse() function −#include using namespace std; //function to reverse given string void reverse_str(string& str){    int n = str.length();    for (int i = 0; i < n / 2; i++)       swap(str[i], str[n - i - 1]); } int main(){    string str = "tutorialspoint";    reverse_str(str);    cout

Read More

Maximum Side Length of a Square with Sum Less than or Equal to Threshold in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 287 Views

Suppose we have a m x n matrix mat and an integer threshold. We have to the maximum side-length of a square with a sum less than or equal to the given threshold or return 0 if there is no such square. So if the input is like −113243211324321132432113243211324321132432And threshold is 4, then output will be 2, as there are two squares of side length 2, so max is 2To solve this, we will follow these steps −Define a method called ok, this will take x and matrix m and threshold thset curr := 0for i in range x – ...

Read More

Maximum Number of Occurrences of a Substring in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 687 Views

Suppose we have a string s, we have to find the maximum number of occurrences of any substring that satisfies the following rules −The number of distinct characters in the substring must be less than or equal to maxLetters.The substring size must be in range minSize and maxSize inclusive.So if the input is like − “aababcaab”, maxLetters = 2, minSize = 3 and maxSize = 4, then the output will be 2. The substring "aab" has 2 occurrences in the original string. This satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).To solve this, we will ...

Read More

Deepest Leaves Sum in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 295 Views

Suppose we have a binary tree, we have to find the sum of values of its deepest leaves. So if the tree is like −Then the output will be 15.To solve this, we will follow these steps −Define a map m, and maxDepthDefine a recursive method solve(), this will take node and level, initially level is 0if node is not present, then returnmaxDepth := max of level and maxDepthincrease m[level] by value of nodesolve(left of node, level + 1)solve(right of node, level + 1)In the main method, setup maxDepth := 0, then solve(root, 0)return m[maxDepth]Example(C++)Let us see the following implementation ...

Read More

Does C++ compiler create default constructor when we write our own?

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 252 Views

In this tutorial, we will be discussing a program to understand if the C++ compiler creates a default constructor when we write our own.Generally, the C++ compiler uses the default constructor when no one is defined, but always uses the one defined by the user if any.Example#include using namespace std; class myInteger{ private:    int value;    //other functions in class }; int main(){    myInteger I1;    getchar();    return 0; }OutputCompiles successfullyExample#include using namespace std; class myInteger{    private:       int value;    public:       myInteger(int v) //user-defined constructor    { value = v; ...

Read More

All Elements in Two Binary Search Trees in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 324 Views

Suppose we have two binary search trees, we have to return a list of values, that has all elements present in these trees, and the list elements will be in ascending order. So if the trees are like −Then the output will be [0, 1, 1, 2, 3, 4].To solve this, we will follow these steps −Define an array called ans, define two stacks st1 and st2curr1 := root1 and curr2 := root2insert node root1 and all left nodes into st1, insert node root2 and all left nodes into st2while st1 is not empty or st2 is not emptyif st1 ...

Read More

Matrix Block Sum in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 522 Views

Suppose we have one m * n matrix called mat and an integer K, we have to find another matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K

Read More

Sum of Nodes with Even-Valued Grandparent in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 265 Views

Suppose we have a binary tree, we have to find the sum of values of nodes with even-valued grandparent. (A grandparent of a node is the parent of its parent, if it exists.). If there are no such nodes with an even-valued grandparent, then return 0. So if the tree is like −The output will be 18. The red nodes are nodes with even-value grandparent, while the blue nodes are the even valued grandparents.To solve this, we will follow these steps −Define a map called parentDefine a method called solve(), this will take node and parif node is null, then ...

Read More
Showing 1891–1900 of 5,962 articles
« Prev 1 188 189 190 191 192 597 Next »
Advertisements