空字符常量 [英] Empty Character Constant

查看:309
本文介绍了空字符常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以绕过Visual Studio Community 2015上的错误C2137?我正在使用 stringstream 删除字符,但是我不想替换它们(即使使用空格也是如此),所以我想删除它们,如果我想删除所有'o''cool'中变成'cl'不是'c l'。我在Stroustrup的书中看到他写了 if(...)ch =''; ,但是我的编译器向我返回错误,而我最好的代理还是空白,这仍然是无法接受的。

Is there a way to bypass error C2137 on Visual Studio Community 2015? I am removing characters with stringstream but I do not want to replace them (even with a blank space), I want to erase them so, if I want to remove all 'o' in 'cool' it becomes 'cl' and not 'c l'. I saw in Stroustrup's book he wrote a if (...) ch = ''; but my compiler returns me an error and my best proxy is white space that's still unacceptable.

这是我使用C2137的功能:

Here's my function with C2137:

string rem_vow(string& s)
{
    for (char& c : s)
    {
        switch (c)
        {
        case 'A': case 'a': case 'E': case 'e': case 'I': 
        case 'i': case 'O': case 'o': case 'U': case 'u':
            c = '';
            break;
        default:
            break;
        }
    }

    return s;
}

编辑:

那是我在书中看到的代码:

That's the code I saw in the book:

预先感谢

推荐答案

否,要删除字符串中的字符,您需要要将字符串的其余部分移动一步,您不能简单地将其替换为空字符。您可以使用 erase 方法,但是在迭代字符串时可能不应该这样做。

No, in order to remove a character in a string you will have to move the rest of the string one step, you cannot simple replace it with "empty" character. You could use the erase method though, but then you should probably not do that while iterating the string.

什么您可能应该做的是遍历原始字符串时构建一个新字符串,例如:

What you probably should do is to build a new string as you traverse the original string, something like:

string rem_vow(string const& s)
{
    string res;

    for (char c : s)
    {
        switch (c)
        {
        case 'A': case 'a': case 'E': case 'e': case 'I':
        case 'i': case 'O': case 'o': case 'U': case 'u':
            //c = ' ';
            break;
        default:
            res.push_back(c);
            break;
        }
    }

    return res;
}

这篇关于空字符常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆