Found 7197 Articles for C++

Why we should avoid using std::endl in C++

Aman Kumar
Updated on 15-May-2025 15:52:30

731 Views

In this article, we will see why we should avoid the std::endl while printing lines into the console or a file. We use std::endl to create a new line after the current line. For a few lines of I/O operations, it is not causing any problems. However, a large number of I/O tasks decreases performance. Why We Avoid Using std::endl There are the following reasons to avoid endl: The endl is used to create new lines, but it does not send to the new line only; after sending the cursor to the next line, it ... Read More

Calculate range of data types using C++

Aman Kumar
Updated on 15-May-2025 16:01:08

4K+ Views

Here, we are going to learn how we can calculate the range of the different C++ data types such as signed data types (int, char, float, etc.) and unsigned data types (unsigned char, unsigned int, unsigned float, etc.). Calculating Range of Signed Data Types In C++, signed data types are used to represent both positive and negative integer values. So, to display their range, we use the following method − Calculate the total number of bits, multiply the sizeof bytes by 8. Calculate -2^(n-1) for minimum range ... Read More

Print a character n times without using loop, recursion or goto in C++

Aman Kumar
Updated on 16-May-2025 16:58:12

1K+ Views

In this article, we will see how to print a character n times without using loops and recursion in C++. Input/Output Scenario Let's see following input output scenario − Input : n = 10, c = 'i' Output : iiiiiiiiii Input : n = 5, character = 'j' Output : jjjjj Using String Constructor Here, using the string constructor in C++ to print a character n times: It allows initialization of a string with multiple copies of a specific character by passing the number of times and the character itself as arguments. Example In this C++ example, we ... Read More

C++ Program to Construct an Expression Tree for a given Prefix Expression

Aman Kumar
Updated on 21-May-2025 14:45:41

5K+ Views

What is Expression Tree?An expression tree is a binary tree used to represent expressions. In an expression tree, internal nodes correspond to operators, and each leaf node corresponds to an operand. Let's see an expression and construct a tree for [5 + ((4+3)*2)]. Constructing an Expression TreeOur task is to construct an expression tree from a prefix expression. We have given a character array arr[] representing a prefix expression, so we have to build an expression tree for the expression and then display the infix and postfix expressions of the created tree. Input/Output Scenario Following are the examples to ... Read More

Array class in C++

Aman Kumar
Updated on 20-May-2025 18:49:03

3K+ Views

Array Class In C++, the array class is part of the standard library and is known for its fixed size. The C++ array class, introduced in C++11, offers a better alternative to C-style arrays. The following are the advantages of the array class over a C-style array: Array class knows its size, whereas a C-style array does not have its size. So when passing to functions, we don't need to pass the size of the array as a separate parameter. C-style array there is more risk of array being decayed into ... Read More

Difference between std::vector and std::array in C++

Aman Kumar
Updated on 15-May-2025 15:56:01

24K+ Views

Both vectors and arrays are used to store collections of elements, but they differ significantly in how they manage their memory and flexibility. C++ std::vector A vector is a dynamic array that can be resized automatically when elements are added or removed. It is a part of the C++ STL and provides more flexibility than a static array. Example In the following example, we will demonstrate the usage of the vector in C++ − #include #include using namespace std; int main() { vector > v { { 4, 5, 3}, {2, 7, 6}, {3, 2, 1, 10} }; cout

Difference between namespace and class in C++

Aman Kumar
Updated on 18-Jun-2025 18:44:13

6K+ Views

In this article, we will see the differences between namespace and class in C++. Namespace and classes are two different concepts, so let's discuss them: Classes are datatypes. It is an expanded version of the structures. Classes can contain data members and functions as members, but namespaces can contain variables and functions by grouping them into one and cannot be created as objects; it is used as additional information to differentiate similar functions, classes, variables, etc. Variables, functions with the same name can be placed in different namespaces. What is Namespace? The namespace is a feature that provides a way ... Read More

C++ Program to implement Gift Wrapping Algorithm in Two Dimensions

Aman Kumar
Updated on 16-May-2025 16:56:07

535 Views

Gift Wrapping AlgorithmThe Gift Wrapping algorithm is also known as Jarvis's march. It is a method for calculating the convex hull of a set of points in a plane. It is essential to find the smallest convex polygon that encloses all the points. Why We Use Gift Wrapping Algorithm? Below are the following reasons to use this algorithm: Easy to Understand: It work like wrapping a string around points. Good for Small Data Sets: When fewer points make up the convex hull. Accurate: Never misses a point ... Read More

C++ Program to Check if a Point d Lies Inside or Outside a Circle Defined by Points a, b, c in a Plane

Aman Kumar
Updated on 16-May-2025 16:57:30

294 Views

In this articles, we implements a C++ Program to check if a point d lies inside or outside a circle defined by points a, b, c in a plane using the following equation: s = (x-xt)^2 + (y-yt)^2 – r*r Where equation contains the following points: x, y: Any point in the plane. xt, yt: Center of the circle. r: Radius of the circle. s: The difference between the squared distance and ( r^2 ). If s is equal to 0, the ... Read More

C++ Program to use above below primitive to test whether two lines intersect

Anvi Jain
Updated on 30-Jul-2019 22:30:25

215 Views

Here is a C++ program to use above below primitive to test whether two lines intersect. It can be used to test whether a line intersects a line segment. It does if and only if one endpoint of the segment is to the left of the line and the other is to the right.AlgorithmBegin    For generating equation of the first line, generate random numbers for coefficient of x and y by using rand at every time of compilation.    For generating equation of the second line, generate random numbers for coefficient of x and y by using rand at ... Read More

Advertisements