#include <iostream>
#include <stack>
using namespace std;
void reverseSentence(string s)
{
stack<int> st;
for (int i = 0; i < s.length(); i++)
{
string word = "";
while (s[i] != ' ' && i < s.length())
{
word += s[i];
i++;
}
st.push(word);
}
while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
cout << endl;
}
int main()
{
string s;
cin >> s;
reverseSentence(s);
return 0;
}
图中显示了push()、pop()、empty()函数中显示的错误我目前使用的是MacOS Ventura 13.1,如果有人能帮助我,那就太好了。
由于函数不工作,我不能为我的代码使用STL模板。
这不起作用,因为您正在创建stack<int>
而您正在推动string
,模板在编译时进行评估,编译器根据您提供的类型生成代码。所以你用int
创建堆栈,你推string
,所以stack<int>
没有push
来代替string
。修改stack<int>
为stack<string>