Found 7401 Articles for C++

Execute both if and else statements simultaneously in C/C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

127 Views

In this section we will see how to execute the if and else section simultaneously in a C or C++ code. This solution is little bit tricky.When the if and else are executed one after another then it is like executing statements where if-else are not present. But here we will see if they are present how to execute them one after another.Example Code#include using namespace std; int main() {    int x = 10;    if(x > 5)   {       lebel_1: cout

C++ Program to Implement AVL Tree

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

19K+ Views

AVL tree is a self-balancing Binary Search Tree where the difference between heights of left and right subtrees cannot be more than one for all nodes.Tree rotation is an operation that changes the structure without interfering with the order of the elements on an AVL tree. It moves one node up in the tree and one node down. It is used to change the shape of the tree, and to decrease its height by moving smaller subtrees down and larger subtrees up, resulting in improved performance of many tree operations. The direction of a rotation depends on the side which ... Read More

Difference between ++*p, *p++ and *++p in c++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

1K+ Views

In this section we will see what are the differences between *ptr++, *++ptr and ++*ptr in C++.Here we will see the precedence of postfix++ and prefix++ in C or C++. The precedence of prefix ++ or -- has higher priority than dereference operator ‘*’ and postfix ++ or -- has priority higher than both prefix ++ and dereference operator ‘*’.When ptr is a pointer, then *ptr++ indicates *(ptr++) and ++*prt refers ++(*ptr)Example Code Live Demo#include using namespace std; int main() { char arr[] = "Hello World"; char *ptr = arr; ++*ptr; cout

Implementing own Hash Table with Open Addressing Linear Probing in C++

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

854 Views

A hash table is a data structure which is used to store key-value pairs. Hash function is used by hash table to compute an index into an array in which an element will be inserted or searched.Linear probing is a collision resolving technique in Open Addressed Hash tables. In this method, each cell of a hash table stores a single key–value pair. If a collision is occurred by mapping a new key to a cell of the hash table that is already occupied by another key. This method searches the table for the following closest free location and inserts the ... Read More

Throwing exceptions from C++ constructors

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

1K+ Views

This is a simple example of throwing exceptions from C++ constructorsAlgorithmClass descriptions and pseudocodes:Begin    Declare a class sample1.       Declare a constructor of sample1.          Print “Construct an Object of sample1”       Declare a destructor of sample1.          Print “Destruct an Object of sample1”    Declare a class sample.       Declare a constructor of sample2.          Declare variable i of the integer datatype.          Initialize i = 7.          Print “Construct an Object of sample1”.       ... Read More

C++ Program to Implement Hash Tables with Quadratic Probing

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

2K+ Views

A hash table is a data structure which is used to store key-value pairs. Hash function is used by hash table to compute an index into an array in which an element will be inserted or searched. Quadratic probing is a collision resolving technique in Open Addressed Hash tables. It operates by taking the original hash index and adding successive values of an arbitrary quadratic polynomial until an open slot is found.This is a C++ program to Implement Hash Tables with Quadratic Probing.AlgorithmFor search a key value:Begin    Declare function SearchKey(int k, HashTable *ht)       int pos = ... Read More

Variable number of arguments in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

5K+ Views

Sometimes, you may come across a situation, when you want to have a function, which can take variable number of arguments, i.e., parameters, instead of predefined number of parameters. The C/C++ programming language provides a solution for this situation and you are allowed to define a function which can accept variable number of parameters based on your requirement. The following example shows the definition of such a function.int func(int, ... ) { . . . } int main() { func(1, 2, 3); func(1, 2, 3, ... Read More

How do I clear the cin buffer in C++?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

2K+ Views

In this section, we will see how to clear the cin buffer in C++. Before entering into that discussion, let us see what is the buffer in C++.The buffer is a temporary storage area. All standard IO devices uses buffers to hold data while they are working. In C++ also the stream is basically the buffer. When we press some key, that does not send to the program. They are stored into buffers. It is buffered by the operating system till the time is allotted to the program.Sometimes we need to clear the unwanted buffer, so when next input is ... Read More

Heap overflow and Stack overflow

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

441 Views

Heap OverflowHeap is used to store dynamic variables. It is a region of process’s memory. malloc(), calloc(), resize() all these inbuilt functions are generally used to store dynamic variables.Heap overflow occurs when −A) If we allocate dynamic large number of variables −int main() {    float *ptr = (int *)malloc(sizeof(float)*1000000.0)); }B) If we continuously allocate memory and do not free after using it.int main() {    for (int i=0; i

What uses are there for “placement new” in C++?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

125 Views

In this section we will see what is the placement new operator in C++. This placement new is another variation of new operator. The normal new operator performs two things. It allocates memory, and then constructs an object in allocated memory.The new operator allocates memory in the heap section and constructs objects there. But for the placement new operator, it constructs object at the given address. To deallocate memory, we can use delete keyword if the memory is allocated using new operator. But for placement new there is no placement delete feature.So in a nutshell, placement new allows you to ... Read More

Advertisements