Not getting any compile error, but I'm getting an output that looks like:
-858993460 5 4 3 2
Instead of 5 4 3 2 1. What am I doing wrong?
#include <iostream>
#include <stack>
using namespace std;
template <typename T>
void reverse(stack<T> &sk)
{
stack<int> tempStack;
while(!sk.empty())
{
tempStack.push(sk.top());
sk.pop();
}
sk = tempStack;
}
int main()
{
int arr[] = {1,2,3,4,5};
int arrSize = sizeof(arr)/sizeof(int);
stack<int> intStack;
for(int i = arrSize; i>0; i--)
{
intStack.push(arr[i]);
}
reverse(intStack);
while(!intStack.empty())
{
cout << intStack.top() << " ";
intStack.pop();
}
return 0;
}