In this tutorial, we will be discussing a program to understand INT_MAX and INT_MIN in C/C++.INT_MIN and INT_MAX are macros that are defined to set the minimum and maximum value for a variable/element.Example Live Demo#include int main(){ printf("%d", INT_MAX); printf("%d", INT_MIN); return 0; }Output2147483647 -2147483648ApplicationCalculating MIN value in an arrayExample Live Demo#include //calculating minimum element in an array int compute_min(int arr[], int n){ int MIN = INT_MAX; for (int i = 0; i < n; i++) MIN = std::min(MIN, arr[i]); std::cout
In this tutorial, we will be discussing a program to understand integer literal in C/C++ (Prefixes and suffixes).Integer literals are literals for integer values directly represented in the source code. Further, they are of two types −Prefixes − Prefixes denotes the base of the value. For example, 0x10 indicates hexadecimal value with 0x.Suffixes − Suffixes denotes the type of the value. For example, 8465484156155LL denotes a long long integer.Example Live Demo#include using namespace std; int main(){ //prefixes cout
In this tutorial, we will be discussing a program to understand the insertion sort using C++ STL.In this we use std::upper_bound to find the element at the wrong position, then rotate the unsorted part of the array to make it sorted.Example Live Demo#include //function to perform insertion sort void insertionSort(std::vector &vec){ for (auto it = vec.begin(); it != vec.end(); it++){ auto const insertion_point = std::upper_bound(vec.begin(), it, *it); std::rotate(insertion_point, it, it+1); } } //printing the array void print(std::vector vec){ for( int x : vec) std::cout
In this tutorial, we will be discussing a program to understand the insertion and deletion in STL set in C++.The set is a container element. The properties that make it unique is that it can only contain unique elements and they can be looped over in a sorted manner.ExampleInsertion Live Demo#include #include using namespace std; int main(){ set st; //declaring iterators set::iterator it = st.begin(); set::iterator it1, it2; pair< set::iterator,bool> ptr; //inserting a single element ptr = st.insert(20); if (ptr.second) cout
In this tutorial, we will be discussing a program to understand input iterators in C++.Input iterators are one of the five iterators in STL being the weakest and simplest of all. They are mostly used in serial input operations where each value is read one and then the iterator moves to the next one.Example Live Demo#include #include using namespace std; int main(){ vector v1 = { 1, 2, 3, 4, 5 }; //declaring iterator vector::iterator i1; for (i1 = v1.begin(); i1 != v1.end(); ++i1) { //looping over elements via iterator cout
MethodHandles class introduced in Java 7 version. This class primarily added some static methods to better the functionality, and falls into several categories like Lookup methods that help to create method handles for methods and fields, Combinator methods that combine or transform pre-existing method handles into new ones, and factory methods to create method handles that emulate other common JVM operations or control flow patterns. MethodHandles class has enhanced in Java 9 to introduce many changes and added new static methods like arrayLength(), arrayConstructor(), zero(), and etc.Syntaxpublic class MethodHandles extends ObjectExampleimport java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; public class MethodHandlesTest { public void MethodHandle1() { ... Read More
Let us first create a collection with documents −> db.demo293.insertOne({FirstName:"Chris", LastName:"Brown", Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d45075d93261e4bc9ea32") } > db.demo293.insertOne({FirstName:"David", LastName:"Miller", Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d45265d93261e4bc9ea33") } > db.demo293.insertOne({FirstName:"John", LastName:"Smith", Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d45385d93261e4bc9ea34") } > db.demo293.insertOne({FirstName:"Adam", LastName:"Doe", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d46cf5d93261e4bc9ea35") }Display all documents from a collection with the help of find() method −> db.demo293.find();This will produce the following output −{ "_id" : ObjectId("5e4d45075d93261e4bc9ea32"), "FirstName" : "Chris", "LastName" : "Brown", "Age" : 24 } { "_id" : ObjectId("5e4d45265d93261e4bc9ea33"), ... Read More
For this, check using $where in MongoDB. Let us create a collection with documents −> db.demo292.insertOne({FirstName:"Chris", LastName:"Brown", ... "Friend":{FirstName:"David", "LastName":"Miller"} ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4c10aa5d93261e4bc9ea30") } > db.demo292.insertOne({FirstName:"John", LastName:"Doe", ... "Friend":{FirstName:"Mike", "LastName":"Doe"} ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4c10dc5d93261e4bc9ea31") }Display all documents from a collection with the help of find() method −> db.demo292.find();This will produce the following output −{ "_id" : ObjectId("5e4c10aa5d93261e4bc9ea30"), "FirstName" : "Chris", "LastName" : "Brown", "Friend" : { "FirstName" : "David", "LastName" : "Miller" } } { "_id" : ObjectId("5e4c10dc5d93261e4bc9ea31"), "FirstName" : "John", ... Read More
To get the sum of two columns, use $add. Let us create a collection with documents −> db.demo291.insertOne({"Value1":10, "Value2":50}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c0e1e5d93261e4bc9ea2f") }Display all documents from a collection with the help of find() method −> db.demo291.find();This will produce the following output −{ "_id" : ObjectId("5e4c0e1e5d93261e4bc9ea2f"), "Value1" : 10, "Value2" : 50 }Following is the query to get the sum of two columns and save it to another column “Value3” −> db.demo291.aggregate( ... { "$project" : { ... 'Value1' : '$Value1', ... 'Value2' : '$Value2', ... 'Value3' : ... Read More
For exact match, you can use $exists that checks for a match. Let us create a collection with documents −> db.demo290.insertOne({"ListOfName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c0c9e5d93261e4bc9ea2d") } > db.demo290.insertOne({"ListOfName":["Chris", "David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c0cb05d93261e4bc9ea2e") }Display all documents from a collection with the help of find() method −> db.demo290.find();This will produce the following output −{ "_id" : ObjectId("5e4c0c9e5d93261e4bc9ea2d"), "ListOfName" : "Chris" } { "_id" : ObjectId("5e4c0cb05d93261e4bc9ea2e"), "ListOfName" : [ "Chris", "David" ] }Here is the query for exact match of a value −> db.demo290.find({$and: [{'ListOfName.0': {$exists: false}}, {"ListOfName": 'Chris'}]});This will produce the ... Read More