Ayush Gupta

Ayush Gupta

433 Articles Published

Articles by Ayush Gupta

Page 24 of 44

How to quickly swap two arrays of the same size in C++?

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 296 Views

In this tutorial, we will be discussing a program to understand how to quickly swap two arrays of same size in C++.For this we will be using a quick method called std::swap() for swapping the elements of the two given arrays.Example#include #include using namespace std;    int main (){    int a[] = {1, 2, 3, 4};    int b[] = {5, 6, 7, 8};    int n = sizeof(a)/sizeof(a[0]);    swap(a, b);    cout

Read More

negative_binomial_distribution in C++ with Examples

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 231 Views

In this tutorial, we will be discussing a program to understand negative_binomial_distribution in C++.This function follows the negative Binomial discrete distribution and produces integers according to this random distribution.Example#include using namespace std; int main() {    //setting number of experiments    const int exps = 10000;    const int numberstars = 100;    default_random_engine generator;    negative_binomial_distribution distribution(4, 0.5);    int p[10] = {};    for (int i = 0; i < exps; ++i) {       int counting = distribution(generator);       if (counting < 10)          ++p[counting];    }    cout

Read More

How to store Data Triplet in a Vector in C++?

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 720 Views

In this tutorial, we will be discussing a program to understand how to store a Data triplet in a vector in C++.To store three elements in a single cell of a vector we will creating a user defined structure and then make a vector from that user defined structure.Example#include using namespace std; struct Test{    int x, y, z; }; int main(){    //creating a vector of user defined structure    vector myvec;    //inserting values    myvec.push_back({2, 31, 102});    myvec.push_back({5, 23, 114});    myvec.push_back({9, 10, 158});    int s = myvec.size();    for (int i=0;i

Read More

Swapping of subranges from different containers in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 129 Views

In this tutorial, we will be discussing a program to understand swapping of subranges of different containers in C++.For this we will be provided with vectors and lists, and we need to swap some of their elements.Example#include #include #include #include using namespace std; int main(){    vector v = { -10, -15, -30, 20, 500 };    list lt = { 10, 50, 30, 100, 50 };    swap_ranges(v.begin(), v.begin() + 3, lt.begin());    for (int n : v)       cout

Read More

Menu Driven C++ Program for a Simple Calculator

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 1K+ Views

In this tutorial, we will be discussing a program to create a menu driven program for a simple calculator.This program will give user the ability to choose among the following mathematical operations − addition, subtraction, multiplication, division, HCF and LCM.Example#include using namespace std; //displaying the menu void menu(){    cout

Read More

Templates and Static variables in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 676 Views

In this tutorial, we will be discussing a program to understand templates and static variables in C++.In case of function and class templates, each instance of the templates has its own local copy of the variables.Example#include using namespace std; template void fun(const T& x){    static int i = 10;    cout

Read More

Maximum of all Subarrays of size k using set in C++ STL

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 270 Views

In this tutorial, we will be discussing a program to get maximum of all subarrays of size k using set in C++ STL.For this we will be provided with a array of size N and integer K. Our task is to get the maximum element in each K elements, add them up and print it out.Example#include using namespace std; //returning sum of maximum elements int maxOfSubarrays(int arr[], int n, int k){    set q;    set::reverse_iterator it;    //inserting elements    for (int i = 0; i < k; i++) {       q.insert(pair(arr[i], i));    }   ...

Read More

Customizing termination behavior for uncaught exception In C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 392 Views

In this tutorial, we will be discussing a program to customize behavior for an uncaught exceptions in C++.Usually, the exception is handled by the try-catch block, but there are instances where there isn’t a matching catch block and the program just terminates. This terminate() function is modifiable as per user requirements.Example#include #include using namespace std; //defining custom terminator void myhandler(){    cout

Read More

Different methods to reverse a string in C/C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 3K+ Views

In this tutorial, we will be discussing a program to understand different methods to reverse a string in C/C++.ExampleUser-defined reverse() function −#include using namespace std; //function to reverse given string void reverse_str(string& str){    int n = str.length();    for (int i = 0; i < n / 2; i++)       swap(str[i], str[n - i - 1]); } int main(){    string str = "tutorialspoint";    reverse_str(str);    cout

Read More

Does C++ compiler create default constructor when we write our own?

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 245 Views

In this tutorial, we will be discussing a program to understand if the C++ compiler creates a default constructor when we write our own.Generally, the C++ compiler uses the default constructor when no one is defined, but always uses the one defined by the user if any.Example#include using namespace std; class myInteger{ private:    int value;    //other functions in class }; int main(){    myInteger I1;    getchar();    return 0; }OutputCompiles successfullyExample#include using namespace std; class myInteger{    private:       int value;    public:       myInteger(int v) //user-defined constructor    { value = v; ...

Read More
Showing 231–240 of 433 articles
« Prev 1 22 23 24 25 26 44 Next »
Advertisements