Saturday, 8 October 2016

C++ program to implement Stack Using Vector in STL


/*
Problem Statement: C++ program to implement Stack Using Vector in STL
Author: Pavan R Jaiswal

Options: 
  1: Push Element to Stack
  2: Pop element from stack
  3. Display stack

Note:  We can't traverse (iterate) through stack. Stacks are a type of container adaptor, specifically designed to operate in a LIFO 
 context (last-in first-out), where elements are inserted and extracted only from one end of the container. Elements 
 are pushed/popped from the "back" of the specific container, which is known as the top of the stack. It is not 
 intended for stack to show this behavior, for this we have other containers like vector. In case of vector use push_back()
 and pop_back() functions.

 reverser_iterator class is called in below code to traverse the vector in reverse orde. 
*/

#include "iostream"
#include "vector"
#include "stdlib.h" // used for exit(0)

using namespace std;

class StackUsingVectorSTL{
 public:
  vector<int> pushToStack(vector<int>);
  vector<int> popFromStack(vector<int>);
  void displayStack(vector<int>);
};

Thursday, 6 October 2016

C++ program to implement Sorting Container in STL


/*
Problem Statement: C++ program to implement Sorting Container in STL
Author: Pavan R Jaiswal

Options: 
  1: Add Element to List
  2: Display Sorted List
*/

#include "iostream"
#include "list"
#include "stdlib.h" // used for exit(0)

using namespace std;

class SortingContainerSTL{
 public:
  list<int> addElementToList(list<int>);
  void displaySortedList(list<int>);
};

Wednesday, 5 October 2016

C++ program to implement Map using STL


/*
Problem Statement: C++ program to implement Map using STL
Author: Pavan R Jaiswal

Options: 
  1: Add Element
  2: Delete Element
  3: Display Map
*/

#include "iostream"
#include "map"


using namespace std;

class MapSTL{
 public:
  map<int, string> addElement(map<int,string>);
  map<int, string> deleteElement(map<int,string>); 
  void displayMap(map<int, string>);
};

C++ program to implement List using STL


/*
Problem Statement: C++ program to implement List using STL
Author: Pavan R Jaiswal

Options: 
  1: Add Element At Front
  2: Add Element At Back
  3: Delete Element From Front
  4: Delete Element From Back
  5: Display List
*/

#include "iostream"
#include "list"

using namespace std;

class ListSTL{
 public:
  list<int> addElementAtFront(list<int>);
  list<int> addElementAtBack(list<int>);
  list<int> deleteElementFromFront(list<int>);
  list<int> deleteElementFromBack(list<int>);
  void displayList(list<int>);
};

Tuesday, 4 October 2016

Function template to perform selection sort on integer and float array in C++

/*
Problem Statement: Function template to perform selection sort on integer and float array in C++ 
Author: Pavan R Jaiswal
*/
#include "iostream"
using namespace std;

class SelectionSort{
 public:
 template <typename type> //keyword "typename" can be replaced by keyword "class" 
  type selectionSort(type tarr[5]){
//   cout<<tarr[4];
   int min;
   float temp;
   for(int i=0;i<5;i++){
    min=i;
    for(int j=i+1;j<5;j++){
     if(tarr[min] > tarr[j]){
      min=j;
     }
    }
    temp = tarr[min];
    tarr[min] = tarr[i];
    tarr[i] = temp;
   }
   cout<<"\nSorted array: "<<endl;
   for(int cnt=0; cnt<5; cnt++) 
    cout<<tarr[cnt]<<endl;
  }
};

Function Template in C++


/*
Title: Function template in C++
Author: Pavan R Jaiswal
 
In C++, function templates are functions that serve as a pattern for creating other similar functions. \
The basic idea behind function templates is to create a function without having to specify the exact type(s) 
of some or all of the variables. Instead, we define the function using placeholder types, called template type
parameters.

Problem Statement: Use function template to check greater number from given two numbers
*/
#include "iostream"
using namespace std;

class FunctionTemplate{
 public:
 template <typename type> //keyword "typename" can be replaced by keyword "class" 
  type greaterNumber(type tNum1, type tNum2){
   return (tNum1>tNum2)?tNum1:tNum2;
  }
};