Problem statement is to create a set of stacks that acts like a pile of plates. Pile of plates can withstand certain height. When it can't withstand we have to create a new set. Implement similar behavior in the code for stack. Use template to implement type independent stack.
#include <iostream>
#include "vector"
#include "string"
using namespace std;
template<typename a>
class setofStacks{
vector<a> setofStacks;
int sizeofEachStack;
public:
void push(a value);
a pop();
a popAt(int index);
};
template<typename a>
void setofStacks<a>::push(a value){
setofStacks.push_back(value);
}
template<typename a>
a setofStacks<a>::pop(){
a val = setofStacks.back();
setofStacks.pop_back();
return val;
}
template<typename a>
a setofStacks<a>::popAt(int index){
int idx = index * sizeofEachStack;
if(setofStacks.size() < idx)return
}
int main()
{
setofStacks<string, 2> a;
a.push("first string");
a.push("second string");
cout<<a.pop();
return 0;
}
Good way to understand the stacks and templates with examples
ReplyDelete