Found 7197 Articles for C++

Middle of three using minimum comparisons in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

641 Views

In this section, we will see how to find the middle of three given values by comparing them. So if three numbers are given like (10, 30, 20), then it will find 20 as this is the middle element. Let us see the algorithm first, then we will implement that algorithm into C++ code.Algorithmmiddle_of_three(a, b, c): Input: Three numbers a, b and c Output: The middle of these three Begin    if a > b, then       if b > c, then          return b       else if a > c, then   ... Read More

Why “using namespace std” is considered bad practice in C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

2K+ Views

C++ has a standard library that contains common functionality you use in building your applications like containers, algorithms, etc. If names used by these were out in the open, for example, if they defined a queue class globally, you'd never be able to use the same name again without conflicts. So they created a namespace, std to contain this change.The using namespace statement just means that in the scope it is present, make all the things under the std namespace available without having to prefix std:: before each of them.While this practice is okay for example code, pulling in the ... Read More

sqrt, sqrtl and sqrtf in C++

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

898 Views

In the cmath library of C++, there are different functions for getting the square root except from sqrt. The sqrt is used basically for double type input. The others are used for float, long type data etc. Let us see the usage of these functions.The sqrt() FunctionThis function is used for double type data. So this returns square root of type double. The syntax is like below.double sqrt(double argument)Example#include #include #include using namespace std; main() {    double x = 144.0;    double y = 180.0;    cout

abs(), labs(), llabs() functions in C/C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

201 Views

In the cstdlib library of C++, there are different functions for getting the absolute value except from abs. The abs are used basically for int type input in C, and int, long, long long in C++. The others are used for long, and long long type data etc. Let us see the usage of these functions.The abs() FunctionThis function is used for int type data. So this returns the absolute value of the given argument. The syntax is like below.int abs(int argument)Example#include #include #include using namespace std; main() {    int x = -145;    int y = 145;    cout

remainder() in C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

207 Views

Here we will see the functionality of remainder() method of C++. The remainder() function is used to compute the floating point remainder of numerator/denominator.So the remainder(x, y) will be like below.remainder(x, y) = x – rquote * yThe rquote is the value of x/y. This is rounded towards the nearest integral value. This function takes two arguments of type double, float, long double, and returns the remainder of the same type, that was given as argument. The first argument is numerator, and the second argument is the denominator.Example#include #include using namespace std; main() {    double x = ... Read More

wcstoll() function in C/C++

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

176 Views

The wcstoll() function is used to convert the wide character string to long long integers. It sets the pointer to point to the first character after the last one. The syntax is like below.long long wcstoll(const wchar_t* str, wchar_t** str_end, int base)This function takes three arguments. These arguments are like below −str: This is the starting of a wide string.str_end: The str_end is set by the function to the next character, after the last valid character, if there is any character, otherwise null.base: This specifies the base. The base values can be of (0, 2, 3, …, 35, 36)This function ... Read More

strtol() function in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

343 Views

The strol() function is used to convert the string to long integers. It sets the pointer to point to the first character after the last one. The syntax is like below. This function is present into the cstdlib library.long int strtol(const char* str, char ** end, int base)This function takes three arguments. These arguments are like below −str: This is the starting of a string.str_end: The str_end is set by the function to the next character, after the last valid character, if there is any character, otherwise null.base: This specifies the base. The base values can be of (0, 2, ... Read More

ldexp() function in C/C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

201 Views

Here we will see what is the use of ldexp() method in C or C++. This function returns any variable x raise to the power of exp value. This takes two arguments x and exp.The syntax is like below.float ldexp (float x, int exp) double ldexp (double x, int exp) long double ldexp (long double x, int exp) double ldexp (T x, int exp)Now let us see one example to get a better idea.Example#include #include using namespace std; int main() {    double a = 10, res;    int exp = 2;    res = ldexp(a, exp); // Finds a*(2^exp)    cout

Preventing Object Copy in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

287 Views

In C++, when classes are created, we can copy it using some copy constructor or assignment operator. In this section we will see, how to prevent object copy of a class in C++. To prevent object copy, we can follow some rules. These are like below.1. Creating private copy constructor and private assignment operator.Example#include using namespace std; class MyClass {    int x;    public:       MyClass() {          //non-parameterized constructor       }       MyClass(int y): x(y) {       }    private:       MyClass(const MyClass& obj) ... Read More

Advanced C++ with boost library

Samual Sam
Updated on 30-Jul-2019 22:30:26

1K+ Views

C++ boost libraries are widely useful library. This is used for different sections. It has large domain of applications. For example, using boost, we can use large number like 264 in C++.Here we will see some examples of boost library. We can use big integer datatype. We can use different datatypes like int128_t, int256_t, int1024_t etc. By using this we can get precision up to 1024 easily.At first we are multiplying two huge number using boost library.Example#include #include using namespace boost::multiprecision; using namespace std; int128_t large_product(long long n1, long long n2) {    int128_t ans = (int128_t) n1 * ... Read More

Advertisements