递归函数-删除特殊字符

Recursion Functions - Removing special characters

本文关键字:特殊字符 删除 递归函数      更新时间:2023-10-16

我需要一个函数来删除字符串中的特殊字符。我正在创建一个程序,接收一个句子,计算元音,并确定其是否为回文。但如果有特殊字符,我需要删除。我使用的是lambda,但它与我使用的编译器不兼容,这就是我的教授希望我们的程序编译的地方。所以,如果有人有其他我可以使用的功能,我会非常感激。以下是我得到的错误:错误:应在â[âtoken之前使用主表达式错误:在â]âtoken之前需要主表达式错误:应在"char"之前使用主表达式我对上面错误所在的行进行了评论。

 #include<iostream> 
    #include <cmath>
    #include <algorithm>

    using namespace std;
    //Create a structure called Sentence
    struct Sentence
    {
        int CountVowels(string , int);
        public:
        Sentence (string);
        bool isPal(string , int);
        void Print();
        string s;
        int numVowel;
        int length;
        //~Sentence();
    };
    Sentence :: Sentence (string b)
    {
        s = b;
        length = 0;
        numVowel = CountVowels(s, 0);
    }
    //Count Vowels function using recursion 
    int Sentence :: CountVowels(string myWord, int startindex)
    {
        length ++;
        int pandi; 
        if(myWord[startindex])
        {
            if (myWord[startindex] != 'a' && myWord[startindex] != 'e' && myWord[startindex] != 'i' && myWord[startindex] != 'o' && myWord[startindex] != 'u')
            {
                pandi = 0;
            }
        else pandi = 1;
        return pandi + CountVowels(myWord, startindex + 1);
        } 
        return 0;
    }
    // Check if it palindorme using recursion 
    bool Sentence :: isPal(string myWord, int size)
    {
        int r = myWord.size() - size;
        int t = size - 1;
        //size = r will be true whenn the size of the string is even and the 2 middle characters have been checked
        if (size == r || r == t)
            return true;
        //r = t will be true when the size of the string is odd and the two characters on either side of the middle character have been checked

        if (tolower(myWord[r]) != tolower(myWord[t]))
            return false;

        return isPal(myWord, -- size);
    }
    //Display the sentence 
    void Sentence :: Print()
    {
        cout << s [-- length];
        if (length == 0)
        {
            cout << "" << endl;
            return;
        }
        Print ();
    }
    //Main function 
    int main ()
    {
        //Holds user sentence 
        string userW;
        //Ask user to enter a sentence 
        cout << "Enter a sentence: n";
        getline(cin, userW);
        //Removes special characters 
        //This is where the ERRORS are 
        userW.erase(remove_if(userW.begin(), userW.end(), [](char c) 
        {return !isalpha(c); }), userW.end());
        //Creates userSent under Sentence 
        Sentence userSent(userW);
        //Display the number of vowels
        cout << "The number of vowels in the sentence is " << userSent.numVowel << endl;
        cout << "" << endl;
        //Display if the sentence is a palindrome or not 
        cout << "The sentence" << " is" << 
        (userSent.isPal(userSent.s, userSent.s.size()) ? " Palindromen" : " not Palindromen");
        cout << "" << endl; 
        //Display the sentence backwards 
        cout << "The sentence spelled backwards is: " << endl;
        userSent.Print();
        return 0;
    }
lambda只是定义类的一种简写方式。如果您选择的话,您总是可以定义一个不带lambda的类似类。例如,像这样的lambda表达式1(其中ab分别属于AB类型):
[&a, =b](char c) { return a.x() + b > c; }

可以定义为一个显式类,比如:

class foo { 
    A mutable &a;
    B b;
public:
    foo(A &a, B b) : a(a), b(b) {}
    bool operator()(char c) const { return a.x() + b > c; }
};

这显然要详细得多(这也是添加lambda表达式的很多原因),但做的事情大致相同。


1.这并不是为了有用,只是为了同时包含两种捕获参数。

Jeffry的回答解释了如果需要的话如何避免lambda。然而,在这个特殊的例子中,有一行不需要lambda。将有问题的行替换为:

  userW.erase(std::copy_if(userW.begin(), userW.end(), userW.begin(), isalpha), userW.end());

如果它不起作用,那么编译器就有STL问题,而不仅仅是lambda问题。