|
When Qian Neng talked about the stack section in C ++, he only talked about the principle of the stack in theory.The actual implementation of the code operation was not mentioned. It was passed over and left only an example of the order of the compartments. I can't understand it, the example is as follows:
#include <iostream>
#include <fstream>
#include <sstream>
#include <stack>
using namespace std;
int main ()
{ifstream in ("rail.txt");
for (int n, line = 0; in >> n&&n&&in.ignore ();)
{cout << line ++? "\n": "";
for (string s; getline (in, s)&&s! = "0";)
{istringstream sin (s);
stack <int> st;
for (int last = 0, coach; sin >> coach; st.pop ())
(for (int p = last + 1; p <= coach; p ++) st.push (p);
if (last <coach) last = coach;
if (st.top ()! = coach) break;
}
cout << (! sin? "yes\n": "no\n");
}
}
}
rail.txt:
5
3 2 1 5 4
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0
The result is:
yes
no
yes
The pop () in this program has just defined a stack, and nothing is pushed onto the stack. What's so popular about pop?
I also set a last variable, I really don't understand what the author meant, so I didn't understand it at all. After watching it all day, I saw that I hit the wall with my head n times. I explain this procedure, especially stack <int> st;
for (int last = 0, coach; sin >> coach; st.pop ())
(for (int p = last + 1; p <= coach; p ++) st.push (p);
if (last <coach) last = coach;
if (st.top ()! = coach) break;
}
cout << (! sin? "yes\n": "no\n");
Thank you very much for telling me some stack functions that I may use frequently in the future. |
|