递归函数-删除特殊字符
Recursion Functions - Removing special characters
我需要一个函数来删除字符串中的特殊字符。我正在创建一个程序,接收一个句子,计算元音,并确定其是否为回文。但如果有特殊字符,我需要删除。我使用的是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;
}
a
和b
分别属于A
和B
类型):
[&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问题。
相关文章:
- 如何用转义符替换字符串中的所有特殊字符
- 转换特殊字符(UTF-8)
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- 如何在C++中打印此"█"特殊字符?
- c++为什么ifstream get()函数返回无法识别的特殊字符
- 如何检查字符串是否包含所有这些:数字、字母和特殊字符
- 转义std::字符串中的特殊字符
- SDL2加载具有特殊字符的文件
- 将 std::string 转换为具有特殊字符的 FString (TCHAR / wstring)
- 如何从文本文件C++读取整数和特殊字符
- USB序列号显示特殊字符
- 在 cpp 中验证路径:无法使用 ASCII 代码删除特殊字符
- 使用迭代器从字符串中删除特殊字符
- 读取 std::string,从 std::string 中删除所有特殊字符
- 从 c 字符串中删除空格和特殊字符
- 从字符串中删除特殊字符C++(' 和 - 除外)
- 递归函数-删除特殊字符
- c++如何从文件中删除所有的字符和特殊字符
- 删除字符数组中后面的特殊字符和字符串
- 从字符串向量中删除特殊字符