如何从char数组中正确删除字符(有无转换为字符串)? [英] How to properly remove a character from a char array (with or without converting to a string)?

查看:96
本文介绍了如何从char数组中正确删除字符(有无转换为字符串)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于任何可能重复的问题,我们深表歉意.我一直在寻找针对我的编译器错误的解决方案,一个星期以来,我在这里尝试了各种解决方案,但仍收到一些错误.

我目前正在学习C ++,试图构建一个程序来执行诸如元音/辅音计数,字母去除等基本操作.一切正常,直到我进入自定义字母去除部分.基本上,使用字符函数(根据我的知识)几乎不可能做到这一点,而转换为字符串似乎会产生其他类型的错误.

I am currently learning C++, trying to build a program that does basic stuff like vowel/consonant count, letter removal etc. Everything works fine until I get to the custom letter removal part. Basically, it's near impossible to do that using character functions (according to my knowledge), while converting to a string seems to spawn other kinds of errors.

这是我不断收到错误的代码片段:

Here is the code fragment where I keep getting errors:

if (strcmp (service, key4) ==0)
{
string str(s);

cout<<endl<<"Please insert the letter you would like removed from your "<<phrasal<<":"<<endl;

cin>>letterToRemove;

s.erase(remove(s.begin(), s.end(),letterToRemove), s.end());

cout<<endl<<s<< "\n"<<endl;
}

这是我使用的初始化变量:

and here are initialized variable I used:

int main()

{
char s[1000], phrasal[10], service[50], key1[] = "1", key2[] = "2", key3[] = "3", key4[] = "4", key5[] = "5", key6[] = "6", key0[] = "0", *p, letterToRemove;

int vowel=0, vowel2=0, consonant=0, consonant2=0, b, i, j, k, phrase=0, minusOne, letter, idxToDel;

void pop_back();

char *s_bin;

如您所见,原始的是一个char数组.在第一个代码示例中,我尝试将其转换为字符串数组( string str(s)),但这会导致以下编译错误:

As you can see, the original 's' is a char array. In the first code sample I have tried converting it into a string array (string str(s)), but that results in the following compiling errors:

  • 错误:请求以非类类型'char [1000]'的's'中的成员'erase'
  • 错误:请求成员'be'中的's',这是非类类型'char [1000]'
  • 错误:请求's'中的成员'end',该成员是非类类型'char [1000]'
  • 错误:请求's'中的成员'end',该成员是非类类型'char [1000]'

我尝试过的另一种解决方法是:

The other workaround I've tried was this:

if(strcmp(service, key4)==0)

{std::string s(s);

cout<<endl<<"Please insert the letter you would like removed from your "<<phrasal<<":"<<endl;

cin>>letterToRemove;

s.erase(remove(s.begin(), s.end(),letterToRemove), s.end());

cout<<endl<<s<< "\n"<<endl;
}

现在这是有趣的部分,对于这一部分,我没有任何错误,但是,一旦选择自定义字母删除功能,调试就会崩溃.它是这样的:

Now here's the funny part, I get no errors whatsoever for this one, but the debug crashes as soon as I select the custom letter removal feature. Here's what it says:

终止在引发'std :: length_error'实例后调用what():基本字串:: _ S_create该应用程序已请求运行时以一种异常方式终止它.请与应用程序的支持团队联系以获取更多信息."

任何帮助将不胜感激,现在已经停留在此一周了!

Any help would be much appreciated, stuck at this one for a week now!

P.S.如果我或主持人在回答此问题后将其删除,可以吗?我敢肯定,我不是第一个提出这个问题的人,但是,正如前面提到的,即使遵循类似问题的答案,我仍然会遇到这些错误.

推荐答案

对于char数组,您必须使用 std :: remove 而不是 erase ,并手动插入null终止符:

For a char array you have to use std::remove instead of erase, and insert the null-terminator manually:

auto newEnd = std::remove(std::begin(s), std::end(s), letterToRemove);
*newEnd = '\0';

这篇关于如何从char数组中正确删除字符(有无转换为字符串)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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