| |

VerySource

 Forgot password?
 Register
Search
View: 688|Reply: 3

Regarding the confusion of the stack, I hope everyone can help.

[Copy link]

3

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-2-17 23:00:01
| Show all posts |Read mode
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.
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-4-21 22:45:01
| Show all posts
Which multi-layer circulation ~~
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-4-22 09:45:01
| Show all posts
for (int last = 0, coach; sin >> coach; st.pop ())
         {for (int p = last + 1; p <= coach; p ++) st.push (p);

Other parts of the code are gone,
Just these two sentences:
for (int last = 0, coach; sin >> coach; st.pop ())
The pop here is a statement that is executed after each execution of the for loop body,
It should be noted that in the loop body is (for (int p = last + 1; p <= coach; p ++) st.push (p); ...
Here is push ya ~~

In other words,
The program is pushed into the stack in the for loop body first, and the loop body is completed after several pushes, and the third statement of for is executed, which is pop. Naturally, there are elements that can be popped
(Please note that the for loop body is another for loop body,
  In other words, push X elements to pop an element out)
Reply

Use magic Report

3

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-7-6 11:15:01
| Show all posts
Thank you! But why not input yes to the first row of data 5 and the fifth row of data 6?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list