Found 7197 Articles for C++

Measure execution time with high precision in C/C++

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

2K+ Views

To create high precision timer we can use the chrono library. This library has high resolution clock. This can count in nanoseconds.In this program we will see the execution time in nanoseconds. We will take the time value at first, then another time value at the last, then find the difference to get elapsed time. Here we are using blank loop to pause the effect for sometimes.Example#include #include typedef std::chrono::high_resolution_clock Clock; main() {    auto start_time = Clock::now();    for(int i = 0; i

How to find Segmentation Error in C & C++ ? (Using GDB)

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

1K+ Views

The segmentation error is one of the runtime error, that is caused because of the memory access violation, like accessing invalid array index, pointing some restricted address etc. In this article, we will see how to detect this type of error using the GDB tool.Let us see the code and respective steps to locate the error.Example#include main() {    int* ptr = NULL;    *ptr = 1; //trying to access unknown memory location    printf("%p", ptr); }Compile the code using ‘gcc –g program_name.c’, and run using ‘./a.out’Outputsoumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$ ./a.out Segmentation fault (core dumped)The segmentation error occurred.Write ‘gdb ./a.out core’soumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$ gdb ... Read More

mbrtowc() function in C/C++

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

227 Views

This mbrtowc() function is used to convert multibyte sequence to wide character string. This returns the length of the multibyte characters in byte. The syntax is like below.mbrtowc (wchar_t* wc, const char* s, size_t max, mbstate_t* ps)The arguments are −wc is the pointer which points where the resulting wide character will be stored.s is the pointer to multibyte character string as inputmax is the maximum number of bytes in s, that can be examinedps is pointing to the conversion state, when interpreting multibyte string.Example#include using namespace std; void display(const char* s) {    mbstate_t ps = mbstate_t(); // initial ... Read More

getopt() function in C to parse command line arguments

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

9K+ Views

The getopt() is one of the built-in C function that are used for taking the command line options. The syntax of this function is like below −getopt(int argc, char *const argv[], const char *optstring)The opstring is a list of characters. Each of them representing a single character option.This function returns many values. These are like below −If the option takes a value, then that value will be pointed by optarg.It will return -1, when no more options to procesReturns ‘?’ to show that this is an unrecognized option, it stores it to optopt.Sometimes some options need some value, If the ... Read More

Errors in C/C++

Tapas Kumar Ghosh
Updated on 28-Jul-2025 15:15:22

13K+ Views

In C/C++, an error occurs due to an invalid operation performed by the user. The error normally stops the program execution until it is fixed. So, the error should be removed before compilation and execution. Types of Error in C/C++ Following is the list of errors occur in C/C++ programming: Syntax Error Run-Time Error Linker Error Logical Error Semantic Error In this article, we will see the implementation of error in C/C++ programs. Syntax Error The syntax error ... Read More

Comparator function of qsort() in C

Anvi Jain
Updated on 14-Feb-2025 18:09:25

2K+ Views

In C, sorting arrays is a common task, and one of the functions that help with sorting is qsort(). To sort the array, qsort() needs a comparator function to decide how to compare two elements. Without this function, qsort() wouldn't know how to order the data. The task here is to understand how to write and use a comparator function with qsort() to sort arrays in different orders. For example, consider this array of integers: arr[] = {5, 2, 9, 1, 5, 6} We want to sort this array in ascending or descending order. The comparator function tells qsort() ... Read More

C++ Program to Implement Trie

Farhan Muhamed
Updated on 15-Jul-2025 18:04:38

2K+ Views

A Trie, also known as a prefix tree, is used to store and search for large sets of strings. In this section we will discuss all about Trie data structure, its operations, and how to implement it in C++. Trie Data Structure A Trie is a data structure similar to a tree that is used to store a dynamic set of strings. The root node represents an empty string, and each edge represents a character. The path from the root to a node represents a prefix of a string stored in the Trie. The image below show how a ... Read More

C++ Program to Find number of Ways to Partition a word such that each word is a Palindrome

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

204 Views

Here we shall discuss a C++ Program to find number of ways to Partition a word in such a way that each word is a Palindrome.AlgorithmsBegin    Take the word as input.    Function partitionadd(vector &u, string &s, vector &tmp, int index):    if (index == 0)       tmp.clear()    for i = index to length-1       st = st + s[i]       if (checkPalin(st))          tmp.push_back(st)          if (i+1 < length)             partitionadd(u, s, tmp, i+1)          else   ... Read More

C++ Program to Find the Longest Prefix Matching of a Given Sequence

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

544 Views

Here we shall discuss a C++ program to find the Longest Subsequence Common to All Sequences in a Set of Sequences.AlgorithmsBegin Take the array of strings as input. function matchedPrefixtill(): find the matched prefix between string s1 and s2 :    n1 = store length of string s1.    n2 = store length of string s2.    for i = 0, j = 0 to i

C++ Program to Find the Shortest Supersequence that Contains Two or more Sequences as Subsequences

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

171 Views

Here we shall discuss a C++ Program to find the Shortest Supersequence that contains two or more Sequences as Subsequences.AlgorithmBegin    function ShortestSubSeq() returns supersequence of A and B:    1) Declare an array ss[i][j] which contains length of shortest supersequence for A[0 .. i-1] and B[0 .. j-1].    2) Find the length of the possible supersequence in bottom up manner using recursion.    3) Declare an array ss[i][j] which stores length of shortest supersequence for A[0 .. i-1] and B[0 .. j-1] in index.    4) Declare a string s to store the shortest subsequence.    5) Initialize ... Read More

Advertisements