Found 7347 Articles for C++

How to determine the version of the C++ standard used by the compiler?

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

4K+ Views

Sometimes we need to know that, what is the current C++ standard. To get this kind of information, we can use the macro called __cplusplus. For different standards, the value of this will be like below.Standard__cplusplus outputC++ pre C++981C++98199711LC++98 + TR1This cannot be checked, this will be marked as C++98C++11201103LC++14201402LC++17201703LExample#include int main() {    if (__cplusplus == 201703L)       std::cout

How to output colored text to a Linux terminal?

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

8K+ Views

Here we will see how to print some lines into the linux terminal with some color. Here we are doing anything special into C++ code. We are just using some linux terminal commands to do this. The command for this kind of output is like below.\033[1;31m Sample Text \033[0mThere are some codes for text styles and colors. These are listed below.ColorForeground CodeBackground CodeBlack3040Red3141Green3242Yellow3343Blue3444Magenta3545Cyan3646White3747Some additional options are like below −OptionCodeDescriptionReset0Back to normal (remove all styles)Bold1Bold the textUnderline4Underline textInverse7Interchange colors of background and foregroundBold off21Normal from boldUnderline off24Normal from UnderlineInverse off27Reverse of the InverseExample#include using namespace std; main() {    cout Read More

What is difference between instantiating a C++ object using new vs. without new?

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

6K+ Views

In C++, we can instantiate the class object with or without using the new keyword. If the new keyword is not use, then it is like normal object. This will be stored at the stack section. This will be destroyed when the scope ends. But for the case when we want to allocate the space for the item dynamically, then we can create pointer of that class, and instantiate using new operator.In C++, the new is used to dynamically allocate memory.Example#include using namespace std; class Point {    int x, y, z;    public:       Point(int x, ... Read More

How to get time in milliseconds using C++ on Linux?

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

4K+ Views

Here we will see how to get time (the elapsed time for the program or any other kind of time).Here we are using linux library for C++. There is a structure called timeval. This timeval stores the time in seconds, milliseconds. We can create two time for start and end, then find the difference from them.Example#include #include #include using namespace std; main() {    struct timeval start_time, end_time;    long milli_time, seconds, useconds;    gettimeofday(&start_time, NULL);    cout > ch;    gettimeofday(&end_time, NULL);    seconds = end_time.tv_sec - start_time.tv_sec; //seconds    useconds = end_time.tv_usec - start_time.tv_usec; //milliseconds ... Read More

Handling large numbers in C++?

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

11K+ Views

In C++, we can use large numbers by using the boost library. This C++ boost library is widely used 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 ... Read More

Generate random numbers following a normal distribution in C/C++

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

2K+ Views

Here we will see how to generate random numbers, which are following in a normal distribution. For the normal random, the formula is like below.𝑧 = √−2 ln 𝑥1 cos (2𝜋𝑥2)Here x1 and x2 are chosen randomly.Example#include #include #include #include using namespace std; double rand_gen() {    // return a uniformly distributed random value    return ( (double)(rand()) + 1. )/( (double)(RAND_MAX) + 1. ); } double normalRandom() {    // return a normally distributed random value    double v1=rand_gen();    double v2=rand_gen();    return cos(2*3.14*v2)*sqrt(-2.*log(v1)); } main() {    double sigma = 82.0;    double Mi = 40.0;    for(int i=0;i

How to get memory usage at runtime using C++?

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

6K+ Views

We can get the memory usage like virtual memory usage or resident set size etc. at run time. To get them we can use some system libraries. This process depends on operating systems. For this example, we are using Linux operating system.So here we will see how to get the memory usage statistics under Linux environment using C++. We can get all of the details from “/proc/self/stat” folder. Here we are taking the virtual memory status, and the resident set size.Example#include #include #include #include #include using namespace std; void mem_usage(double& vm_usage, double& resident_set) {   ... Read More

How to print Unicode character in C++?

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

5K+ Views

To print the Unicode characters, the process is very similar to the actual printing process in C++.We can put the Unicode value with the prefix \u. Thus we can successfully print the Unicode character.Note: If the console does not support Unicode, then you cannot get the correct result. Here we have used the Linux system to solve this problem.Example#include using namespace std; int main() {    cout

How to make the program sleep for x milliseconds in C++?

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

413 Views

Here we will see how to sleep for x (given by user) milliseconds in C++ program.To do this thing we can use different libraries. But here we are using the clock() function. The clock() will return the current CPU time. Here we will try to find the ending time from the clock, and the given x value. Then for that amount of time, we will run one blank while loop to take the time. Here one macro is used called CLOCKS_PER_SEC, this finds the number of clock ticks per second.Let us see the code to get the better idea about ... Read More

What is the difference between static_cast<> and C style casting?

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

3K+ Views

Here we will see what are the differences between static_cast and normal C style cast.The normal cast like (int)x is C style typecasting where static_cast(x) is used in C++.This static_cast() gives compile time checking facility, but the C style casting does not support that. This static_cast() can be spotted anywhere inside a C++ code. And using this C++ cast the intensions are conveyed much better.In C like cast sometimes we can cast some type pointer to point some other type data.Like one integer pointer can also point character type data, as they are quite similar, only difference is character has ... Read More

Advertisements