Found 7347 Articles for C++

How to create an unordered_set of user defined class or struct in C++?

Ayush Gupta
Updated on 25-Feb-2020 07:49:36

551 Views

In this tutorial, we will be discussing a program to understand how to create an unordered set of user defined class or struct in C++.For this we will create a structure type and then compare two structure types with the function defined by the user to store the hash function.Example#include using namespace std; //defined structure struct Test {    int id;    bool operator==(const Test& t) const{       return (this->id == t.id);    } }; //defined class for hash function class MyHashFunction {    public:       size_t operator()(const Test& t) const{         ... Read More

How to create an unordered_map of user defined class in C++?

Ayush Gupta
Updated on 25-Feb-2020 07:36:24

486 Views

In this tutorial, we will be discussing a program to understand how to create an unordered map of user defined class in C++.To create an unordered map from a user defined class, we will pass the hash function as the class method being the third argument.Example Live Demo#include using namespace std; //objects of class to be used as key values struct Person {    string first, last;    Person(string f, string l){       first = f;       last = l;    }    bool operator==(const Person& p) const{       return first == p.first && ... Read More

How to create an unordered_map of pairs in C++?

Ayush Gupta
Updated on 25-Feb-2020 07:31:23

743 Views

In this tutorial, we will be discussing a program to understand how to create an unordered map of pairs in C++.Unordered maps are the ones that don't contain hash function for the pairs by default. If we want a hash value for a particular pair, it needs to be passed on explicitly.Example Live Demo#include using namespace std; //to hash any given pair struct hash_pair {    template    size_t operator()(const pair& p) const{       auto hash1 = hash{}(p.first);       auto hash2 = hash{}(p.second);       return hash1 ^ hash2;    } }; int main(){ ... Read More

How to create a List with Constructor in C++ STL

Ayush Gupta
Updated on 25-Feb-2020 07:28:17

192 Views

In this tutorial, we will be discussing a program to understand how to create a List with constructor in C++ STL.List are data structures to store elements in memory in a non-contiguous fashion. They are insertion and deletion quick as compared to vectors.Example Live Demo#include #include using namespace std; //printing the list void print_list(list mylist){    list::iterator it;    //printing all the elements    for (it = mylist.begin(); it != mylist.end(); ++it)       cout

How to add “graphics.h” C/C++ library to gcc compiler in Linux

Ayush Gupta
Updated on 25-Feb-2020 07:25:35

1K+ Views

In this tutorial, we will be discussing a program to understand how to add “graphics.h” C/C++ library to gcc compiler in Linux.To do this we are required to compile and install the libgraph package.This includes install build-essential and some external packages>>sudo apt-get install build-essential >>sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-2.0 guile-2.0-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-devThen setting the path in the extracted files>>sudo make install >>sudo cp /usr/local/lib/libgraph.* /usr/libExample#include #include #include int main(){    int gd = DETECT, gm;    initgraph(&gd, &gm, NULL);    circle(40, 40, 30);    delay(40000); ... Read More

How does a vector work in C/C++

Ayush Gupta
Updated on 25-Feb-2020 07:20:01

2K+ Views

In this tutorial, we will be discussing a program to understand how vectors work in C/C++.A vector data structure is an enhancement over the standard arrays. Unlike arrays, which have their size fixed when they are defined; vectors can be resized easily according to the requirement of the user.This provides flexibility and reduces the time requirement with arrays to copy the previous elements to the newly created array.Example Live Demo#include #include using namespace std; int main(){    vector myvector{ 1, 2, 3, 5 };    myvector.push_back(8);    //not vector becomes 1, 2, 3, 5, 8    for (auto x : myvector)    cout

Count all sub-sequences having product <= K – Recursive approach in C++

Ayush Gupta
Updated on 17-Feb-2020 10:49:05

151 Views

In this tutorial, we will be discussing a program to find the number of sub-sequences having product k) {       discard_count += power(2, n - i);       return;    }    if (i == n)       return;       float rem = prefix[n - 1] - prefix[i];    if (sum + a[i] + rem > k)       solve(i + 1, n, sum + a[i], k, a, prefix);    if (sum + rem > k)       solve(i + 1, n, sum, k, a, prefix); } int countSubsequences(const int* arr, ... Read More

Count all possible walks from a source to a destination with exactly k edges in C++

Ayush Gupta
Updated on 17-Feb-2020 10:45:04

109 Views

In this tutorial, we will be discussing a program to find the number of walks from a source to a destination with exactly k edges.For this we will be provided with a graph and the values of source and destination. Our task is to find all the possible paths starting from the source to the destination having exactly k edges.Example Live Demo#include using namespace std; #define V 4 //counting walks using recursion int countwalks(int graph[][V], int u, int v, int k){    if (k == 0 && u == v)       return 1;    if (k == 1 && graph[u][v])       return 1;    if (k

Count all prefixes of the given binary array which are divisible by x in C++

Ayush Gupta
Updated on 17-Feb-2020 10:40:52

83 Views

In this tutorial, we will be discussing a program to find the number of prefixes of the binary array which are divisible by x.For this we will be provided with binary array and a value x. Our task is to find the number of elements whose prefixes are divisible by given value x.Example Live Demo#include using namespace std; //counting the elements with prefixes //divisible by x int count_divx(int arr[], int n, int x){    int number = 0;    int count = 0;    for (int i = 0; i < n; i++) {       number = number ... Read More

Count all Quadruples from four arrays such that their XOR equals to ‘x’ in C++

Ayush Gupta
Updated on 17-Feb-2020 10:39:07

123 Views

In this tutorial, we will be discussing a program to find the number of quadruples from four arrays such that their XOR equals to x.For this we will be provided with four arrays and a value x. Our task is to count all the quadruples whose XOR is equal to the given value x.Example Live Demo#include using namespace std; //counting quadruples with XOR equal to x int count_quad(int a[], int b[], int c[], int d[], int x, int n){    int count = 0;    for (int i = 0 ; i < n ; i++)       for (int ... Read More

Advertisements