简体   繁体   中英

STL functions not working in vscode for my Mac

#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;
}

The image shows the error displayed in the push(), pop(), empty() functions I am currently using MacOS Ventura 13.1, it would be great if anyone could help.

Since the functions aren't working, I cannot use the STL templates for my codes.

This is not working because you are creating stack<int> and you are pushing string , templates are compile time evaluated and compiler generate code based on the type you provided. so you are creating stack with int and you are pushing string so stack<int> does not have push for string . Change stack<int> to stack<string> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM