将元素从 std::string 推入字符串类型堆栈时出错

Error while Pushing Elements from a std::string onto a Stack of String Type

在我的 isPalindrome() 函数中尝试循环遍历字符串以将每个元素推入字符串类型 s1 的堆栈时出现此错误。

no instance of overloaded function "std::stack<_Ty, _Container>::push [with _Ty=std::string, _Container=std::deque>]" matches the argument list

当我将位于堆栈顶部的元素分配给字符串变量时,弹出错误:

'std::stack>>::top': non-standard syntax; use '&' to create a pointer to member

为什么提到std::deque

#include <iostream>
#include <string>
#include <stack>
#include <algorithm> 

class Palindrome
{
public:
    Palindrome();
    void inputString();
    std::string convert2lower();
    bool isPalindrome();

private:
    std::string userstring;
    std::stack<std::string> s1;
};

Palindrome::Palindrome()
{}

void Palindrome::inputString()
{
    std::cout << "Enter a string: ";
    std::getline(std::cin,userstring);
}

std::string Palindrome::convert2lower()
{
    userstring.erase(remove(userstring.begin(), userstring.end(), ' '), userstring.end());
    userstring.erase(std::remove_if(userstring.begin(), userstring.end(), ispunct), userstring.end());
    transform(userstring.begin(), userstring.end(), userstring.begin(), tolower);
    return userstring;
}

bool Palindrome::isPalindrome()
{
    size_t n = userstring.size();

    for (size_t i = 0; i < n; ++i)
    {
        s1.push(userstring[i]);
    }

    std::string reversed;
    for (size_t i = 0; i < n; ++i)
    {
        std::string temp = s1.top;
        reversed.insert(i,temp);
        s1.pop();
    }

    if (reversed == userstring) 
    {
        return true;
    }

    return false;
}

int main()
{
    Palindrome p1;

    p1.inputString();

    std::cout << "\nCalling convert2lower(): " << std::endl;
    std::cout << "The new string is " << p1.convert2lower() << std::endl;
    std::cout << "\nCalling isPalindrome(): " << std::endl;

    if (!p1.isPalindrome())
    {
        std::cout << "String is NOT a Palindrome!" << std::endl;
    }
    else
    {
        std::cout << "String is a Palindrome!" << std::endl;
    }
}

这是您的代码,经过绝对最小的更改...

#include <iostream>
#include <string>
#include <stack>
#include <algorithm> 

class Palindrome
{
public:
    Palindrome();
    void inputString();
    std::string convert2lower();
    bool isPalindrome();

private:
    std::string userstring;
    std::stack<char> s1;
};

Palindrome::Palindrome()
{}

void Palindrome::inputString()
{
    std::cout << "Enter a string: ";
    std::getline(std::cin, userstring);
}

std::string Palindrome::convert2lower()
{
    userstring.erase(remove(userstring.begin(), userstring.end(), ' '), userstring.end());
    userstring.erase(std::remove_if(userstring.begin(), userstring.end(), ispunct), userstring.end());
    transform(userstring.begin(), userstring.end(), userstring.begin(), tolower);
    return userstring;
}

bool Palindrome::isPalindrome()
{
    size_t n = userstring.size();

    for (size_t i = 0; i < n; ++i)
    {
        s1.push(userstring[i]);
    }

    std::string reversed;
    for (size_t i = 0; i < n; ++i)
    {
        char temp = s1.top();
        reversed.insert(i, &temp, 1);
        s1.pop();
    }

    if (reversed == userstring)
    {
        return true;
    }

    return false;
}

int main()
{
    Palindrome p1;

    p1.inputString();

    std::cout << "\nCalling convert2lower(): " << std::endl;
    std::cout << "The new string is " << p1.convert2lower() << std::endl;
    std::cout << "\nCalling isPalindrome(): " << std::endl;

    if (!p1.isPalindrome())
    {
        std::cout << "String is NOT a Palindrome!" << std::endl;
    }
    else
    {
        std::cout << "String is a Palindrome!" << std::endl;
    }
}

以下是您问题的答案:

  1. 没有...的实例这是由于使用字符串而不是字符作为堆栈类型造成的。
  2. 非标准语法...这是因为您将 () 去掉了 pop。
  3. 提到双端队列是因为堆栈是双端队列的特化。