Found 7197 Articles for C++

Construct a linked list from 2D matrix (Iterative Approach) in C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:18:10

217 Views

Suppose we have one matrix, we have to convert it to 2d linked list using iterative approach. The list will have the right and down pointer.So, if the input is like102030405060708090then the output will beTo solve this, we will follow these steps −real_head := NULLDefine an array head_arr of size: m.for initialize i := 0, when i < m, update (increase i by 1), do −head_arr[i] := NULLfor initialize j := 0, when j < n, update (increase j by 1), do −p := new tree node with value mat[i, j]if real_head is null, then −real_head := pif head_arr[i] is ... Read More

User Defined Literals in C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:00:41

369 Views

Here we will see the concept of the user-defined literals in C++. From C++ version 11, the User Defined Literals (UDL) are added in C++. C++ also provides literals for a variety of built-in types but these are limited.Built-in Literals −31 (Integer)3.5 (Double)4.2F (Float)'p' (Character)31ULL (Unsigned Long Long)0xD0 (Unsigned Hexadecimal Integer)"pq" (String)Apart from the built-in literals, sometimes we need user defined literals. There are few reasons behind that. Let us see with few examples −Suppose we want to define one weight variable, but we cannot specify the units, like if we define as follows −long double Weight = 3.5;We have ... Read More

Uninitialized primitive data types in C/C++ Program

Akansha Kumari
Updated on 15-Jul-2025 17:10:46

318 Views

In this section we will see when we declare a variable that is un-initialized, then which value they hold in C or C++ language.What Happens When You Don’t Initialize Variables? In C and C++, when a variable is declared inside a function (i.e., as a local variable) but not explicitly initialized, it holds an undefined or garbage value. This means the variable may contain any value that is present at that memory location.Unlike some high-level languages where variables are automatically initialized (e.g., 0 for integers, false for booleans), C and C++ do not initialize local variables by default. So, assuming ... Read More

Uniform Initialization in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:55:32

1K+ Views

Here we will discuss about the uniform initialization in C++. This is supported from C++11 version. The uniform initialization is a feature that permits the usage of a consistent syntax to initialize variables and objects which are ranging from primitive type to aggregates. In other words, it introduces brace-initialization that applies braces ({}) to enclose initializer values.Syntaxtype var_name{argument_1, argument_2, .... argument_n}Initialize Dynamically allocated arraysExample (C++)Let us see the following implementation to get better understanding − Live Demo#include using namespace std; int main() {    int* pointer = new int[5]{ 10, 20, 30, 40, 50 };    cout

Traversing a map (or unordered_map) in C++ STL

Arnab Chakraborty
Updated on 27-Aug-2020 13:51:33

2K+ Views

Here we will see the map container and its use in C++. The maps are defined as associative containers that store elements in a hash-mapped fashion. Each element is associated with a key and value. No two mapped values can have identical keys. These are some fundamental methods that are present inside the map container in C++.begin(): This returns an iterator to the first element in the map.end()− This returns an iterator to the theoretical element that follows last element in the map.size() − This returns the number of elements in the map.max_size() − This returns the maximum number of ... Read More

Geometry using Complex Numbers in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:46:10

322 Views

In this section, we will see how to make point class using complex class from STL in C++. And apply them on some geometry related problems. The complex number is present inside the complex class from STL (#include )Defining Point ClassTo make complex to point, we will change the name of the complex as point, then change x to real() of complex class and y to imag() of complex class. Thus, we can simulate the point class.# include typedef complex point; # define x real() # define y imag()We have to keep in mind that the x and y ... Read More

Generating Test Cases (generate() and generate_n() in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:43:39

310 Views

In this section we will see how we can use C++ STL function to generate test cases. Sometimes generating test cases for array programs can be very complicated and inefficient process. C++ provides two methods to generate test cases. These methods are as follows −The generate() methodThe C++ function std::algorithm::generate() assigns the value returned by successive calls to gen to the elements in the range of first to last. It takes three parameters first, last and gen, these are forward iterator to the initial position, backward iterator to the final position and generator function that is called with no argument, ... Read More

Gaussian Filter Generation in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:41:20

2K+ Views

As we know the Gaussian Filtering is very much useful applied in the field of image processing. It is used to reduce the noise of an image. In this section we will see how to generate a 2D Gaussian Kernel. Gaussian Distribution for generating 2D kernel is as follows.$$G(x,y)= \frac{1}{2\Pi\:\sigma^{2}}e^{\frac{x^{2}+y^{2}}{2\sigma^{2}}}$$ExampleLet us see the following implementation to get better understanding − Live Demo#include #include #include #define PI 3.1415 using namespace std; void calc_filter(double kernel[][5]) {    double sigma = 1.0;    double p, q = 2.0 * sigma * sigma;    double sum = 0.0;    for (int x = -2; x

Computing index using pointers returned by STL functions in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:39:36

174 Views

In this section we will see how to generate index using pointers that are returned by STL in C++. As we know many inbuilt functions in C++ return the pointers to the position in memory which provides the address of desired number, but it has no relation with the actual index in the container of the returned value. As an example, to determine maximum element in a code, we use std::max_element() function, this does not return the index of the desired element, but return the address in memory. But sometimes, we need to get the index from that address. Here ... Read More

How to make a C++ class whose objects can only be dynamically allocated?

Arnab Chakraborty
Updated on 27-Aug-2020 13:37:16

356 Views

In this problem we will see how we can make one class for which we can only create objects through dynamic memory allocation, no direct object creation is permitted.The idea is simple. We have to create private destructor for that class. When the destructor is private, the compiler would produce a compiler error for non-dynamically allocated objects because compiler need to remove them from stack segment once they are not available for use. For dynamically allocated objects, the programmer is responsible for deleting object, but the compiler is not responsible for that, so it permits creating objects dynamically.For avoiding memory ... Read More

Advertisements