In C++, a std::vector is a container that is a part of the STL (Standard Template Library). It enables dynamic arrays which can adjust their size automatically. Depending on the requirements and version of C++ being used, there are many ways to initialize a std::vector with hardcoded elements. Initializing a std::vector with Hardcoded Elements Some of the approaches to initialize a std::vector with hardcoded elements in C++: Using Initializer List Using assign() Function Using accumulate() function Initializing Vector Using Initializer List An initializer list is a ... Read More
In C++, declaration and definition are often confused. A declaration tells the compiler about the name and type, while a definition allocates memory or provides implementation. In this article, we will understand their differences with examples. What is a Declaration in C++? A declaration means (in C or C++) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user-defined type or function in your program. No space is reserved in memory for any variable in case of the declaration. Following is the syntax to ... Read More
In this article, we have an unsorted array of integers having repetitive elements. Our task is to count the distinct elements in the given array in C++. Example Here is an example of counting the unique number in the given array: Input: array = {4, 2, 32, 26, 57, 52, 40, 2, 57, 40, 33} Output: Distinct elements in the array: 8 Here is a list of approaches for counting distinct elements in the given array: Using Nested for Loop Using Sorting ... Read More
In this article, we will explain the 4 color problem to color a graph and implement the backtracking algorithm to solve it in C++. The 4 Color Problem The 4-color problem states that the maximum number of colors needed to color any planar graph (or a 2D map) is four, such that no two adjacent nodes have the same color. For example, suppose that you want to color a world map, such that no two countries sharing a border have the same color. According to this theorem, the maximum number of colors needed to do this is four. Now, ... Read More
In this article, we will solve a sum array puzzle, where we have an array with n elements. We have to create another array of n elements such that the 'ith' position of the second array will hold the sum of all elements of the first array except the 'ith' element. We have one constraint, we cannot use the subtraction operator in this problem. Example Here is an example of calculating the sum of array elements except for the ith element: Input: arr[] = {5, 6, 7, 8} Output: res[] = ... Read More
Both C and C++ are powerful programming languages that are used by developers to write both system-level and high-level programs. C language follows procedural programming paradigm with a simple and structured approach, whereas C++ supports both procedural and object-oriented programming. Although both C and C++ are widely used across various fields for developing different types of system and application software but they have different strength and use cases. In this article, we will learn when to use C over C++, and when C++ is a better choice than C. When to use C Language? When ... Read More
In this article, we will see how to get the Host name and IP address of the local system in an easier way using C and C++ program. Getting IP Address of Local Computer For this, some standard networking functions are available in socket programming with header files like on Windows and , , on Linux. In this article we will mainly discuss three commonly used functions: Sr.No ... Read More
A structures (or struct) is a user-defined data type, which is used to group and store variables of different data types inside a single name. But in C, structures can store only data members whereas C++ can store member functions, constructors, destructors, and even inheritance which is similar to classes in C++, but the only difference between struct and classes is that struct members are public by default. In this following article we will see how structure differs in both C and C++ in detail. Structures in C The structures in C is a user-defined data type, which is used ... Read More
In this article, we will learn about the use of the Optional.stream() method in Java 9. First, we will learn about the optional class and its method. Then we will see the use cases along with an example.Optional class The Optional class was introduced in Java 8 to reduce the null pointer exception that occurs in Java. It is basically a container for the actual object that needs to be returned by a method. The point here is that if a method returns a null value, which can be null, then that method could return a value instead contained inside ... Read More
What is JLink? Java Linker is a new linker tool that has been used to create our own customized JRE. Usually, we can run our program using default JRE provided by Oracle. If we need to create our own JRE then use this tool. JLink tool can help to create its own JRE with only the required class to run the application. It can reduce the size of the API developed and the dependency of using the full JRE. In Java 9, we have a new phase between compiling the code and its execution, link time. Link time is an optional phase between the phases of ... Read More