curious question about a loop
#include <cstdlib>
#include <iostream>
#include <stack>
using namespace std;
void unionMerge(stack<int>& S, stack<int>& T);
int main()
{
stack <int> Q;
stack <int> P;
Q.push(5);
Q.push(7);
Q.push(8);
Q.push(9);
P.push(1);
P.push(2);
P.push(4);
P.push(6);
unionMerge(Q, P);
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void unionMerge(stack<int>& S, stack<int>& T)
{
stack <int> tempDescending;
stack <int> mergenonDescending;
for(int x = 0; x<S.size() + T.size(); x++)
{
if(S.size()==0)
{
tempDescending.push(T.top());
T.pop();
}
else if(T.size()==0)
{
tempDescending.push(S.top());
S.pop();
}
else
{
if(S.top()>T.top())
{
tempDescending.push(S.top());
S.pop();
}
else
{
tempDescending.push(T.top());
T.pop();
}
}
}
while(tempDescending.size()!=0)
{
mergenonDescending.push(tempDescending.top());
cout<<mergenonDescending.top();
tempDescending.pop();
}
}
the loop thats bolded, when i write
for(int x = 0; x<S.size() + T.size(); x++)
i get a totally different answer to
for(int x = 0; x<8; x++)
BUT S.SIZE() + T.SIZE() DOES = 8.
but when i write for(int x = S.size() + T.size(); x>0; x--)
i get the same answer as for(int x = 0; x<8; x++) which is the right answer...
why ? thanks for the help

