Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 不删除字符串中间的逗号_C++_String_Formatting_Ifstream - Fatal编程技术网

C++ 不删除字符串中间的逗号

C++ 不删除字符串中间的逗号,c++,string,formatting,ifstream,C++,String,Formatting,Ifstream,我有一个字符串,它包括: 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.
我有下面的代码,应该去掉这个字符串中的所有标点符号。测试变量是我的字符串:

 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函数可以去掉句点,但不能去掉逗号。为什么会这样?提前感谢。

好吧,您这样做只是为了
test[test.length()-1]
(字符串中的最后一个字符)。这里没有逗号,只有句点。

好吧,您只对
test[test.length()-1]
(字符串中的最后一个字符)执行此操作。这里没有逗号,只有句号。

看起来您正在查找(以及)

注: 调用
remove
之后通常会调用容器的方法,该方法会删除未指定的值并减小容器的物理大小以匹配其新的逻辑大小


#include

看起来您正在查找(以及)

注: 调用
remove
之后通常会调用容器的方法,该方法会删除未指定的值并减小容器的物理大小以匹配其新的逻辑大小


#包括

doh!我现在明白了,我想我的问题是什么是测试整个字符串的最佳方法?字符串迭代器是合适的还是应该使用简单的for循环?@NicYoung:不需要显式的
for
-loop。请参阅以获得实现此目的的简洁方法。doh!我现在明白了,我想我的问题是什么是测试整个字符串的最佳方法?字符串迭代器是合适的还是应该使用简单的for循环?@NicYoung:不需要显式的
for
-loop。看到一个整洁的方法来实现这一点。非常好,我不知道删除如果。我必须在我的代码中尝试它。我很高兴你得到了推荐信。RIP dmr。我收到了一个错误,它说,
调用'remove\u if'没有匹配的函数。
@bobthemac:抱歉,我错过了
算法
标题。修复得很好,我不知道是否要移除。我必须在我的代码中尝试它。我很高兴你得到了推荐信。RIP dmr。我收到了一个错误,它说,
调用'remove\u if'没有匹配的函数。
@bobthemac:抱歉,我错过了
算法
标题。修复如果测试为空字符串(即长度为0)这是错误代码@franji1我在测试为空字符串之前有代码。如果测试为空字符串(即长度为0),这是错误代码@franji1我在测试为空字符串之前有代码。
#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;
}