【发布时间】:2018-11-23 12:35:55
【问题描述】:
所以我是 C++ 的新手,并试图创建一个可以从字符串中删除元音的函数,但我失败了,这是我目前的代码:
#include <string>
using namespace std;
string remove(string st) {
for(int i = 0; st[i] != '\0'; i++)
st[i] = st[i] == 'a' || st[i] == 'e' || st[i] == 'i' || st[i] == 'o' || st[i] ==
'u' || st[i] == 'A' || st[i] == 'E' || st[i] == 'I' || st[i] == 'O' || st[i] ==
'U' ? '' : st[i];
}
return st;
这似乎引发了错误?知道我做错了什么
我得到的错误是:
main.cpp:10:16: error: expected expression 'U' ? '' : Z[i];
并在另一个解释器上运行:
.code.tio.cpp:7:14: error: incompatible operand types ('const char *' and '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' (aka 'char'))
'U' ? "" : Z[i];
^ ~~ ~~~~
【问题讨论】:
-
它“抛出”了什么错误?
-
''不是有效的字符值。也许您想使用空格字符或其他东西,但它不会从字符串中“删除”该字符,只需替换它的值。 -
看来
std::remove_if可能是完成这项工作的更好工具。 -
请edit您的问题,并用您所观察到的准确描述替换“似乎引发错误”。
-
请确保错误来自您显示的代码,错误为
"",但代码为''
标签: c++ regex string str-replace