Ayush Gupta

Ayush Gupta

433 Articles Published

Articles by Ayush Gupta

Page 25 of 44

Swapping of subranges from different containers in C++

Ayush Gupta
Ayush Gupta
Updated on 02-Mar-2020 120 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 Live Demo#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

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

Ayush Gupta
Ayush Gupta
Updated on 02-Mar-2020 708 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 Live Demo#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

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

Ayush Gupta
Ayush Gupta
Updated on 02-Mar-2020 280 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 Live Demo#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

How to join two Vectors using STL in C++?

Ayush Gupta
Ayush Gupta
Updated on 02-Mar-2020 359 Views

In this tutorial, we will be discussing a program to understand how to join two given vectors using STL library in C++.To join two given vectors we would be using the set_union() method from the STL library.Example Live Demo#include using namespace std; int main(){    //collecting the vectors    vector vector1 = { 1, 45, 54, 71, 76, 12 };    vector vector2 = { 1, 7, 5, 4, 6, 12 };    sort(vector1.begin(), vector1.end());    sort(vector2.begin(), vector2.end());    cout

Read More

How to convert a class to another class type in C++?

Ayush Gupta
Ayush Gupta
Updated on 25-Feb-2020 5K+ Views

In this tutorial, we will be discussing a program to understand how to convert a class to another class type in C/C++.Class conversion can be done with the help of operator overloading. This allows data of one class type to be assigned to the object of another class type.Example Live Demo#include using namespace std; //type to which it will be converted class Class_type_one {    string a = "TutorialsPoint";    public:       string get_string(){          return (a);    }    void display(){       cout

Read More

How to find the sum of elements of a Vector using STL in C++?

Ayush Gupta
Ayush Gupta
Updated on 25-Feb-2020 895 Views

In this tutorial, we will be discussing a program to understand how to find the sum of elements of a vector using STL in C++.To find the sum of elements of a given vector, we would be using the accumulate() method from the STL library.Example Live Demo#include using namespace std; int main(){    //defining the vector    vector a = { 1, 45, 54, 71, 76, 12 };    cout

Read More

How to find common elements between two Arrays using STL in C++?

Ayush Gupta
Ayush Gupta
Updated on 25-Feb-2020 296 Views

In this tutorial, we will be discussing a program to understand how to find common elements between two arrays using STL in C++.To find the common elements between two given arrays we will be using the set_intersetion() method.Example Live Demo#include using namespace std; int main(){    //defining the array    int arr1[] = { 1, 45, 54, 71, 76, 12 };    int arr2[] = { 1, 7, 5, 4, 6, 12 };    int n1 = sizeof(arr1) / sizeof(arr1[0]);    int n2 = sizeof(arr2) / sizeof(arr2[0]);    sort(arr1, arr1 + n1);    sort(arr2, arr2 + n2);    cout

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 25-Feb-2020 874 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 a List with Constructor in C++ STL

Ayush Gupta
Ayush Gupta
Updated on 25-Feb-2020 277 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

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 25-Feb-2020 2K+ 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
Showing 241–250 of 433 articles
« Prev 1 23 24 25 26 27 44 Next »
Advertisements