我不明白如何在 c++ 中做一个remove_if
I don't understand how to do a remove_if not in c++
这段代码有效,但有点有限,所以如果它不等于一个字母,我想删除一些东西。
我知道我必须使用:isalpha而不是:ispunct,但我不知道如果它不等于::isalpha。我仔细研究了这个问题,但没有得到任何答案,因为我不明白。
textFile[i].erase(remove_if(textFile[i].begin(), textFile[i].end(), ::ispunct), textFile[i].end());
感谢您的帮助。
我还没有编译,但这应该可以工作:
textFile[i].erase(
remove_if(textFile[i].begin(), textFile[i].end(), std::not1(std::ptr_fun(::isalpha))),
textFile[i].end());
这里感兴趣的链接是:
- http://www.cplusplus.com/reference/std/functional/ptr_fun/
- http://www.cplusplus.com/reference/std/functional/not1/
如果标准函子还不够,您还可以实现自己的:
struct not_a_character : std::unary_function<char, bool> {
bool operator()(char c) const {
return !isalpha(c);
}
};
可以用作:
textFile[i].erase(
remove_if(textFile[i].begin(), textFile[i].end(), not_a_character()),
textFile[i].end());
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '