我有一个函数,给定一个文本块,应该删除所有标点符号,并使所有字母小写,并最终根据单字母密码转换它们。下面的代码有效:
class Cipher {
public:
string keyword;
string decipheredText;
deque<string> encipheredAlphabet;
static bool is_punctuation (char c) {
return c == '.' || c == ',' || c == '!' || c == '\''|| c == '?' || c
== ' ';
}
string encipher(string text) {
Alphabet a;
encipheredAlphabet = a.cipherLetters(keyword);
text.erase( remove_if(text.begin(), text.end(), is_punctuation),
text.end() );
string::iterator it;
for (it = text.begin(); it != text.end(); it++) {
*it = tolower(*it);
// encipher text according to shift
}
return text;
}
};
问题是,它目前对字符串进行了两次传递,一次删除标点符号,一次执行所有其他操作。这似乎效率低下,因为似乎所有转换都可以通过字符串以某种方式完成。是否有一种干净的方法可以将擦除删除习语与其他循环条件结合起来?