递归函数 - 删除特殊字符

时间:2015-11-09 03:54:28

标签: c++ recursion lambda

我需要一个删除字符串中特殊字符的函数。我正在创建一个程序,它接受一个句子并计算元音并确定它的回文结构。但是如果有的话,我需要删除特殊字符。我使用的是lambda,但它与我正在使用的编译器不兼容,这是我的教授希望我们的程序编译的地方。因此,如果任何人有我可以使用的其他功能,我会非常感激。 这些是我得到的错误:  错误:在'令牌之前预期的primary-expression  错误:在令牌之前预期的主要表达式  错误:在âcharâ之前预期的primary-expression 我评论了错误所在的上面一行。

 #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()) ? " Palindrome\n" : " not Palindrome\n");
        cout << "" << endl; 
        //Display the sentence backwards 
        cout << "The sentence spelled backwards is: " << endl;
        userSent.Print();

        return 0;
    }

2 个答案:

答案 0 :(得分:3)

lambda只是定义类的简便方法。如果选择,您始终可以定义一个没有lambda的类似类。例如,像 1 这样的lambda表达式(其中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.这并不是有用的,只是包括两种捕获参数。 功能

答案 1 :(得分:0)

Jeffry的回答解释了如果你需要的话,如何避免使用lambda。但是,在这个特殊情况下,有一个不需要lambda的单行。用以下内容替换有问题的行:

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

如果它不起作用,那么你的编译器就会出现STL问题,而不仅仅是lambda。