Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

c++ - Implementation of stack <array<string, 100>>

I have been learning C++ for a couple of weeks now and started making my game where I pumped into this problem with std::stack.

I have a class that have a board and a stack

#include <string>
#include <stack>

array<string, 100> board;
stack<array<string, 100>> history;

each time I create my board or make changes I push it to the stack simply by history.push(board). I have made an undo function that looks like this:

array<string, 100> Board::undoMove(stack<array<string, 100>> &history){
    array<string, 100> arr;
    if(!history.empty()){
        history.pop();
        arr = history.top();
    }
    else{
        arr =  array<string, 100>();
    }
    return arr;
}

when calling it:

array<string,100> tempState = b.undoMove(history);
if(!tempState.empty()){
    board = tempState;
    cout << "
Undoing your latest move.. "<< endl;
}
else{
    cout << "No previous move available" << endl;
}

Apparently, this function works only if I make an undo move after the first change to the board. After a couple of changes, my program stops after a pause. I'm not really good with debugging so I couldn't figure out what is wrong and where. I'm making this game so It helps me understand how stacks work but I don't think this is going anywhere! I think there is something wrong with how I think the stack should work in this case. Also, some people suggested that the fact the stack is a stack of std::array creates the problem when I'm pushing to it but that doesn't sound right, so I'm very confused about how I should look at this problem or think of it and I'm looking for any suggestions I could get.

question from:https://stackoverflow.com/questions/65877310/implementation-of-stack-arraystring-100

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
history.pop();

This removes the element at the top of the stack. As in: it goes away. It ceases to be. It goes to meet its maker. It joins the choir invisible. It pines for the fjords. It is now an ex-element. Afterwards:

arr = history.top();

This returns the current element on the top of the stack. This does not return the ex-element. It's ancient history.

So, in the end: if at the beginning of this adventure the stack had only one element, first it gets removed, then an attempt is made to retrieve the element at the top of a completely empty stack. Which, of course, doesn't exist, and the whole house of cards comes tumbling down.

You did these two operations out of order. First you need to retrieve the element at the top of the stack; and only then, after the element is safely tucked away in your own variable, then you get to pop the stack.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...