Found 7347 Articles for C++

static_assert in C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:55:01

282 Views

static_assert is a function which is useful for programmers to print the error in the screen after compiling the program without messing up with the output too much.Earlier in C++11 and C++14, static_assert had different functionality which means we have to write our own message while defining the static_assert. However, In C++ 17 static_assert can be invoked without passing the message.It is compatible with other asserts libraries functions like BOOST_STATIC_ASSERT as well.Syntax{    auto __range= For-range-Intializer;    auto __begin= begin-expression;    auto __end= end-expression;    for(; __begin!= __end; ++__begin){       range-declaration= *__begin;       statement    } ... Read More

Square of a sorted array in C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:54:29

1K+ Views

In the given array of sorted integers, the task is to print the squares of every array element and print the array in sorted order. For example, Input-1 −arr[ ] = { -3, -1, 0, 1, 4, 6 };Output −{0, 1, 1, 9, 16, 36}Explanation − The squares of each of the elements of the given array [-3, -1, 0, 1, 4, 6 ] is [0, 1, 1, 9, 16, 36 ].Input-2 −arr[ ]= { 0, 1, 2, 8, 9 }Output −{0, 1, 4, 64, 81}Explanation − The squares of each of the elements of the given array [ 0, ... Read More

Sort an arrays of 0’s, 1’s and 2’s using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:53:04

752 Views

Given an array of 0, 1, and 2, sort the elements in an order such that all the zeros come first before 1 and all the 2’s in the end. We have to sort all the elements of the array in-place.We can solve this problem using DNF (Dutch National Flag) Sorting Algorithm. For example, Input-1 −arr[ ]= {2, 0, 0, 1, 2, 1 }Output −0 0 1 1 2 2Explanation − Sorting the given array of elements containing 0, 1 and 2 using DNF Sorting Algorithm, it will print the output as {0, 0, 1, 1, 2, 2}.Input-2 −arr[ ]= ... Read More

Significance of Lambda Function in C/C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:51:35

4K+ Views

Lambda Function − Lambda are functions is an inline function that doesn’t require any implementation outside the scope of the main program.Lambda Functions can also be used as a value by the variable to store. Lambda can be referred to as an object that can be called by the function (called functors).Whenever the compiler encounters a definition of the lambda function, it generally creates a custom object for the lambda.A lambda function has more functionality than a normal function, for example, it has a capturing method to capture the used variables. However, the captured variable is treated as the member ... Read More

Search an element in the given singly Linked List using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:50:20

423 Views

Given a singly linked list, the task is to search a particular element in the linked list. If the element is found, then print “present” otherwise “Not present”. For example, Input-1 −1→ 2→ 3→ 4→ 5→ 6Searching for ‘7’Output −Not PresentExplanation − In the given singly linked list the element ‘7’ is not present, hence we will return the output as “Not Present”.Input-2 −1→ 2→ 3→ 4→ 5Searching for ‘2’Output −PresentExplanation − Since in the given Singly linked list the element ‘2’ is present thus we will return the output as “Present”.Approach to solve this problemThere are two approaches to ... Read More

Reverse Vowels of a string in C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:49:03

2K+ Views

Given a string, the task is to reverse all the vowels present in the given string. For example, Input-1 −a = “tutor”Output −toturExplanation − Reversing the string “tutor” will generate the output as “totur.Input-2 −a = “mathematics”Output −mithametacsExplanation − Reversing the string “mathematics” will generate the output as “mithametacs”.Approach to solve this problemGiven a string, we have to reverse all the vowels present in it. There are several approaches to solve this particular problem but we have to solve this in linear time O(n).Thus, the better method to solve this problem is to use the Two-Pointer approach in which we ... Read More

Write a program in C++ to replace all the 0’s with 5 in a given number

Dev Prakash Sharma
Updated on 05-Feb-2021 12:48:02

1K+ Views

Given an integer N, the task is to replace all the 0’s that appear in the number with ‘5’. However, the number with leading ‘0’ cannot be replaced with ‘5’ as it remains unchanged. For example, Input-1 −N= 1007Output −1557Explanation − The given number has 2 zeros which when replaced by ‘5’ results in the output as 1557.Input-2 −N = 00105Output −155Explanation − Since the given number starts with the leading ‘0’ which can be ignored and the output after replacing the 0 in the middle with ‘5’ results the output as 155.Approach to Solve this ProblemTo replace all the ... Read More

Write a program in C++ to remove duplicates from a given array of prime numbers

Dev Prakash Sharma
Updated on 05-Feb-2021 12:46:48

286 Views

Let us suppose we have given an array N size which consists of all the prime numbers. The task is to find the duplicates in the given array and remove them. For example, Input-1 −N = 8 arr[ ] = { 2 ,2 ,2 ,3 ,3 ,3 ,5 ,7 }Output −2 3 5 7Explanation − In the given array of prime numbers there are some duplicates of ‘2’ and ‘3’ after removing the duplicates the output will be 2 3 5 7.Input-2 −N = 5 arr[ ] = { 3, 2, 7, 5, 5}Output −3 2 7 5Explanation − In ... Read More

Remove comments in a string using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:46:09

807 Views

Given a C++ program as input, remove the comments from it. ‘source’ is a vector where the i-th line of the source code is the source[i]. This represents the result of splitting the source code string by the newline character . In C++, we can create two types of comments, i.e., Line Comments, Block Comments.The string ‘\’ denotes the line comment, which means the string next to it on the right will be ignored by the program.The string ‘\* and *\’ is a multiline comment representing the string starting from ‘\* till the *\’ will be ignored.The first useful comment ... Read More

Maximum Erasure Value in C++

Kiran P
Updated on 04-Feb-2021 12:01:30

167 Views

Given an array of positive integers, the task is to erase a subarray containing all the unique elements. What you get by erasing the subarray is equal to the sum of its elements.Return the maximum sum of the current subarray by erasing the terms before or after it, we can get the maximum sum by erasing exactly one subarray.An array arr is called to be a subarray of a if it forms a contiguous subsequence of a that is if it is equal to a[l], a[l+1], ..., a[r] for some (l, r). For example, Input-1 −arr[ ] = { 1, 2, ... Read More

Advertisements