Programming Articles - Page 2086 of 3363

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

Ayush Gupta
Updated on 25-Feb-2020 09:15:13

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

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

Ayush Gupta
Updated on 25-Feb-2020 09:12:10

870 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

How to find the maximum element of a Vector using STL in C++?

Nishu Kumari
Updated on 19-Aug-2025 17:15:15

7K+ Views

A vector in C++ is a dynamic array that stores elements of the same data type and can change its size when needed. In this article, we are given a vector and our goal is to find the maximum (largest) element using different STL methods in C++. Let's understand this with an example: // Example 1 std::vector vec1 = {11, 13, 21, 45, 8}; The largest element is 45. // Example 2 std::vector vec2 = {1, 9, 2, 5, 7}; The largest element is 9. Finding Maximum Element of a Vector Using STL in ... Read More

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

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

823 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

633 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

What is the purpose of using Optional.ifPresentOrElse() method in Java 9?

Alshifa Hasnain
Updated on 11-Jun-2025 13:41:57

3K+ Views

In this article, we will learn about the purpose of using the Optional.ifPresentOrElse() method in Java 9.  The Optional Class The Optional class was introduced in Java 8. Many Java developers face null pointer exceptions to avoid this, we need to put null checks we need to put if, to check if a method is null we can put it in the if statement; otherwise, we have to put it in the else statement. And to check this, we need to put this at multiple points to avoid this situation, We use the Optional class. It avoids the null pointer ... Read More

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

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

977 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

268 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

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

How does a vector work in C/C++

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

3K+ 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

Advertisements