Ispunct()不删除字符串中间的逗号
ispunct() not removing commas in the middle of a string
我有一个字符串,它由:
UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.
我有下面的代码,应该剥离这个字符串的所有标点符号。测试变量是字符串:
if(std::ispunct(test[test.length()-1]))
{
test.erase(test.length()-1, 1);
}
然而,当我在这个函数之后再次输出这个字符串时,我有以下内容:
UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity
由于某些原因,ispunct函数能够去掉句号,但不能去掉逗号。为什么它会有这样的行为?
看起来您正在寻找remove_if
算法(以及ispunct
谓词)。
N。B:
调用
remove
之后通常会调用容器的erase
方法,该方法会擦除未指定的值并减小容器的物理大小以匹配新的逻辑大小。
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
int main()
{
std::string dmr = "UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.";
auto last = std::remove_if(dmr.begin(), dmr.end(), ispunct);
dmr.erase(last, dmr.end());
std::cout << dmr << std::endl;
}
看它运行!
好吧,您只对test[test.length()-1]
(字符串中的最后一个字符)这样做。这里没有逗号,只有句号
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 递归形成字符串中所有数字字符的中间和?
- 如何组合一个宽字符字符串,中间插入一些空字符
- C++ 我想将两个字符串写入管道,中间有一小段延迟
- 在 txt 文件中搜索中间子字符串未被选中的字符串
- 如何从字符串中间开始的子字符串
- 从中间器转换字符串不起作用,出了什么问题?
- 在std::字符串或字符数组中间包含NULL字符的UPDATE
- 如何分辨 boost::split_iterator 从字符串中间开始
- 如何打印在中间包含0个字符的字符串
- 矢量字符串字符替换在字符串c++的中间
- 我怎么能在C++中的文件名中间使用字符串值呢
- 如何在序列化期间插入字符串流的中间
- 如何为字符串分配一个从数组中间开始的char数组
- 如何使用c++解析文本文件从中间到特定字符串
- 如何清除包含C函数声明的字符串中的注释和中间空白
- Ispunct()不删除字符串中间的逗号
- 在文本文件中间附加字符串
- 从文件中读取整数,中间有一个字符串
- 如何以特定格式将数字打印到字符串的中间