使用 std::erase 和 std::remove_if 函数从 C++ 中的字符串中删除标点符号
在 C++ 中使用自定义函数从字符串中移除标点符号
在 C++ 中使用迭代方法从字符串中删除标点符号
本文将介绍几种在 C++ 中如何从字符串中删除标点符号的方法。
使用 std::erase 和 std::remove_if 函数从 C++ 中的字符串中删除标点符号
std::remove_if 是 STL 算法库的一部分,它可以删除所有给定条件为 true 的元素。条件应返回范围内每个元素的 bool 值。在这种情况下,我们通过 ispunct 函数来检查标点符号。注意,std::remove_if 返回新值范围的通过迭代器,因此我们将使用 erase 方法链接到它的调用,该方法生成已解析的字符串。这个解决方案的工作原理与 erase-remove 习语相似。
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
string text =
"Lorem ipsum, dolor sit! amet, consectetur? adipiscing elit. Ut "
"porttitor.";
cout |