Posts

Showing posts from October, 2016

make pair usuage

The behavior of this function template is the same as if defined as:
1 2 3 4 5template <class T1,class T2> pair<T1,T2> make_pair (T1 x, T2 y) { return ( pair<T1,T2>(x,y) ); }// make_pair example#include <utility> // std::pair#include <iostream> // std::coutint main () { std::pair <int,int> foo; std::pair <int,int> bar; foo = std::make_pair (10,20); bar = std::make_pair (10.5,'A'); // ok: implicit conversion from pair<double,char> std::cout << "foo: " << foo.first << ", " << foo.second << '\n'; std::cout << "bar: " << bar.first << ", " << bar.second << '\n'; return 0; } The difference is that with std::pair you need to specify the types of both elements, whereas std::make_pair will create a pair with the type of the elements that are passed to it, without you needing to tell it. Tha…

BIT MASKS(coding)

I was having trouble understanding Bitmask, then I found an unknown pdf about Bitmask on google. I would like to help the beginners like me in understanding Bitmasks and their uses. Here we go : #### MOTIVATION Suppose you have a set of objects and you want some way to represent which objects to pick and which ones not to
pick. How do you represent that in in a program? More generally, how do you represent a subest of a set?One way is to use a Map to associate with each object a boolean value indicating whether the object is picked. Alternatively,if the object can be indexed by integers, you can use a boolean array. However, this takes up a lot of memory and can be slow due to the overhead of Map and array. If the size of the set is not too large, a bitmask is much more efficient (and convenient)! #### WHAT IS BITMASKS ? Bitmasks a.k.a. lightweight, small sets of Booleans (native support in C/C++/Java). An integer is stored in a computer’s memory as a sequence/string of bits. Thus, we c…

Packages in Java

Packages In Java
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for: Preventing naming conflicts. For example there can be two classes with name Employee in two packages, college.staff.cse.Employee and college.staff.ee.EmployeeMaking searching/locating and usage of classes, interfaces, enumerations and annotations easierProviding controlled access: protected and default have package level access control. A protected member is accessible by classes in the same package and its subclasses. A default member (without any access specifier) is accessible by classes in the same package only.Packages can be considered as data encapsulation (or data-hiding). All we need to do is put related classes into packages. After that we can simply write a import a class from existing packages and use it in our program. A packages is container of group of related classes where some of the classes are accessible are exposed and others are kep…

Coding

Image

stack using recursion

Reverse a stack using recursion You are not allowed to use loop constructs like while, for..etc, and you can only use the following ADT functions on Stack S:
isEmpty(S)
push(S)
pop(S) Solution:
The idea of the solution is to hold all values in Function Call Stack until the stack becomes empty. When the stack becomes empty, insert all held items one by one at the bottom of the stack. For example, let the input stack be 1 <-- top 2 3 4 First 4 is inserted at the bottom. 4 <-- top Then 3 is inserted at the bottom 4 <-- top 3 Then 2 is inserted at the bottom 4 <-- top 3 2 Then 1 is inserted at the bottom 4 <-- top 3 2 1 So we need a function that inserts at the bottom of a stack using the above given basic stack function. void insertAtBottom((): First pops all stack items and stores the popped item in function call stack using recursion. And when stack becomes empty, pushes new item and all items s…